PrevNextUpHome SophiaFramework UNIVERSE 5.3
SFXXMLNode
[XML] Base class which represents a DOM Node.
#include <SFXXMLNode.hpp>
class SFXXMLNode;
SFMTYPEDEFCLASS(SFXXMLNode)

Inheritance diagram

 Inheritance diagram of SFXXMLNodeClass

Description

In SophiaFramework, process the XML document by constructing the DOM tree using the following 12 classes for the 12 types of nodes defined in W3C ( World Wide Web Consortium ), which inherit from the SFXXMLNode class.

Class name Node Node type (nodeType attribute) Node name (nodeName attribute) Node value (nodeValue attribute) Node attributes (attributes)
SFXXMLAttribute Attribute node ATTRIBUTE_NODE name of attribute value of attribute null
SFXXMLCDATASection CDATASection node CDATA_SECTION_NODE "#cdata-section" content of the CDATA Section null
SFXXMLComment Comment node COMMENT_NODE "#comment" content of the comment null
SFXXMLDocument Document node DOCUMENT_NODE "#document" null null
SFXXMLDocumentFragment DocumentFragment node DOCUMENT_FRAGMENT_NODE "#document-fragment" null null
SFXXMLDocumentType DocumentType node DOCUMENT_TYPE_NODE Document type name null null
SFXXMLElement Element node ELEMENT_NODE tag name null set of Attribute nodes(SFXXMLNode::DOMNamedNodeMap type)
SFXXMLEntity Entity node ENTITY_NODE entity name null null
SFXXMLEntityReference EntityReference node ENTITY_REFERENCE_NODE name of entity referenced null null
SFXXMLNotation Notation node NOTATION_NODE notation name null null
SFXXMLProcessingInstruction ProcessingInstruction node PROCESSING_INSTRUCTION_NODE target entire content excluding the target null
SFXXMLText Text node TEXT_NODE "#text" content of the Text node null
[Note] Note
The values of nodeType, nodeName, nodeValue attributes can be obtained by the SFXXMLNode::GetNodeType, SFXXMLNode::GetNodeName, SFXXMLNode::GetNodeValue functions respectively.
[Note] Note
The attributes is valid only for the Element node, and can be obtained by the SFXXMLElement::GetAttributes function.
[Note] Document Object Model (DOM) Specification

W3C DOM Level 2 Specification: Document Object Model (DOM) Level 2 Core Specification

Reference

SFXXMLAttribute | SFXXMLCDATASection | SFXXMLComment | SFXXMLDocument | SFXXMLDocumentFragment | SFXXMLDocumentType | SFXXMLElement | SFXXMLEntity | SFXXMLEntityReference | SFXXMLNode | SFXXMLNotation | SFXXMLProcessingInstruction | SFXXMLText

Member

Constructor/Destructor
SFXXMLNode( SFXXMLDocumentPtr owner )
Constructor of the SFXXMLNode class.
SFXXMLNode( Void )
Constructor of the SFXXMLNode class.
~SFXXMLNode( Void )
Destructor of the SFXXMLNode class.
Public Functions
SFCError AppendChild( SFXXMLNodeConstPtr newchild )
Append the specified child node at the end.
SFCError CloneNode( SFXXMLNodeHandle clonenode , Bool deep )
Create a copy of the node.
Bool EqualNode( SFXXMLNodeConstPtr arg )
Check whether this node equals the specified node or not.
DOMNodeListPtr GetChildNodes( Void )
Get all the child nodes.
SFXXMLNodePtr GetFirstChild( Void )
Get the first child node.
SFXXMLNodePtr GetLastChild( Void )
Get the last child node.
SFXXMLNodePtr GetNextSibling( Void )
Get the next sibling node.
ACharConstPtr GetNodeName( Void )
Get the node name.
NodeType GetNodeType( Void )
Get the node type.
ACharConstPtr GetNodeValue( Void )
Get the node value.
SFXXMLNodePtr GetParentNode( Void )
Get the parent node.
SFXXMLNodePtr GetPreviousSibling( Void )
Get the previous sibling node.
SFXAnsiString GetText( Void )
Get the node text.
Bool HasChildNodes( Void )
Check whether this node has any child nodes or not.
SFCError InsertBefore( SFXXMLNodeConstPtr newchild , SFXXMLNodeConstPtr refchild )
Insert the specified node.
Bool IsSameNode( SFXXMLNodeConstPtr other )
Check whether this node is the same as the specified one or not.
SFCError RemoveChild( SFXXMLNodeConstPtr oldchild )
Remove the specified child node including its descendant nodes.
SFCError ReplaceChild( SFXXMLNodeConstPtr newchild , SFXXMLNodeConstPtr oldchild )
Replace the specified child node.
Void SetNodeValue( ACharConstPtr value )
Set the node value.
SFCError SetText( SFXAnsiStringConstRef text )
Set the node text.
Types
DOMNamedNodeMap
Class to manage the DOM nodes by name
DOMNodeList
Class to manage the DOM nodes by list
NodeType
SFXXMLNode::NodeType is an enumeration type of NodeType values defined in the W3C DOM Level 1 Specification. Document Object Model (DOM) Level 1 Specification )

SFXXMLNode::SFXXMLNode
Constructor of the SFXXMLNode class.
[ public, explicit ]
SFXXMLNode(
    SFXXMLDocumentPtr owner   // the Document node that owns this node
);
[ protected, explicit ]
SFXXMLNode(Void);

SFXXMLNode::~SFXXMLNode
Destructor of the SFXXMLNode class.
[ public, virtual ]
virtual ~SFXXMLNode(Void);

SFXXMLNode::AppendChild
Append the specified child node at the end.
[ public, virtual ]
SFCError AppendChild(
    SFXXMLNodeConstPtr newchild   // child node to append
);

Return value

Return SFERR_UNSUPPORTED.

Reference

SFXXMLNode::RemoveChild | SFXXMLNode::ReplaceChild | SFXXMLNode::CloneNode | SFXXMLNode::InsertBefore


SFXXMLNode::CloneNode
Create a copy of the node.
[ public, virtual ]
SFCError CloneNode(
    SFXXMLNodeHandle clonenode   // destination node(Handle type)
    Bool deep                    // whether or not to copy the child nodes recursively
);

Return value

Return SFERR_UNSUPPORTED.

Reference

SFXXMLNode::RemoveChild | SFXXMLNode::ReplaceChild | SFXXMLNode::AppendChild | SFXXMLNode::InsertBefore


SFXXMLNode::EqualNode
Check whether this node equals the specified node or not.
[ public, virtual, const ]
Bool EqualNode(
    SFXXMLNodeConstPtr arg   // specified node
);

Return value

  • If yes: true
  • Otherwise: false

Description

The SFXXMLNode::EqualNode function check not the node identity(whether or not the reference to the node is same) but the node equality(whether or not the content of the node is same).

To check the node identity, use the SFXXMLNode::IsSameNode method.

Reference

SFXXMLNode::IsSameNode


SFXXMLNode::GetChildNodes
Get all the child nodes.
[ public, virtual, const ]
DOMNodeListPtr GetChildNodes(Void);

Return value

Return null.

Description

Get a pointer to the SFXXMLNode::DOMNodeList instance that contains all child nodes of this node.

[Note] Note
Each element of SFXXMLNode::DOMNodeList is of the "pointer to the SFXXMLNode" type.

Reference

SFXXMLNode::DOMNodeList | SFXXMLNode::GetFirstChild | SFXXMLNode::GetLastChild | SFXXMLNode::GetNextSibling | SFXXMLNode::GetParentNode | SFXXMLNode::GetPreviousSibling


SFXXMLNode::GetFirstChild
Get the first child node.
[ public, virtual, const ]
SFXXMLNodePtr GetFirstChild(Void);

Return value

Return null.

Reference

SFXXMLNode::GetLastChild | SFXXMLNode::GetChildNodes | SFXXMLNode::GetNextSibling | SFXXMLNode::GetPreviousSibling | SFXXMLNode::GetParentNode


SFXXMLNode::GetLastChild
Get the last child node.
[ public, virtual, const ]
SFXXMLNodePtr GetLastChild(Void);

Return value

Return null.

Reference

SFXXMLNode::GetFirstChild | SFXXMLNode::GetChildNodes | SFXXMLNode::GetNextSibling | SFXXMLNode::GetPreviousSibling | SFXXMLNode::GetParentNode


SFXXMLNode::GetNextSibling
Get the next sibling node.
[ public, virtual, const ]
SFXXMLNodePtr GetNextSibling(Void);

Return value

Return null.

Reference

SFXXMLNode::GetPreviousSibling | SFXXMLNode::GetFirstChild | SFXXMLNode::GetLastChild | SFXXMLNode::GetChildNodes | SFXXMLNode::GetParentNode


SFXXMLNode::GetNodeName
Get the node name.
[ public, virtual, const ]
ACharConstPtr GetNodeName(Void);

Return value

Return null.

Description

[Note] Node Name

The node name is the value of nodeName attribute and differs depending on the node type.

e.g. In case of an Element node (nodeType == ELEMENT_NODE), the tag name is returned. In case of a Text node (nodeType == TEXT_NODE), null is returned.


SFXXMLNode::GetNodeType
Get the node type.
[ public, virtual, const ]
NodeType GetNodeType(Void);

Return value

Return UNKNOWN_NODE.

Description

[Note] Node Type

The node type is the value of nodeType attribute and differs depending on the node type.

And it is the same as NodeType defined in the W3C DOM Level1 Specification (www.w3.org/tr/1998/REC-DOM-Level-1-19981001/level-one-core.html) as below.

Integer value Node type
1 ELEMENT_NODE
2 ATTRIBUTE_NODE
3 TEXT_NODE
4 CDATA_SECTION_NODE
5 ENTITY_REFERENCE_NODE
6 ENTITY_NODE
7 PROCESSING_INSTRUCTION_NODE
8 COMMENT_NODE
9 DOCUMENT_NODE
10 DOCUMENT_TYPE_NODE
11 DOCUMENT_FRAGMENT_NODE
12 NOTATION_NODE
-1 UNKNOWN_NODE

Reference

SFXXMLNode::NodeType


SFXXMLNode::GetNodeValue
Get the node value.
[ public, virtual, const ]
ACharConstPtr GetNodeValue(Void);

Return value

Return null.

Description

[Note] Node Value

The node value is the value of nodeValue attribute and differs depending on the node type.

e.g. In case of a Text node (nodeType == TEXT_NODE), the text of the node is returned. In case of an Element node (nodeType == ELEMENT_NODE), null is returned.

Reference

SFXXMLNode::SetNodeValue


SFXXMLNode::GetParentNode
Get the parent node.
[ public, virtual, const ]
SFXXMLNodePtr GetParentNode(Void);

Return value

Return null.

Description

If the node has no parent node, null is returned. (e.g. a node that does not belong to the DOM tree)

All the nodes except the Attribute, Document, DocumentFragment, Entity and Notation nodes always have the parent node.

Reference

SFXXMLNode::GetFirstChild | SFXXMLNode::GetLastChild | SFXXMLNode::GetChildNodes | SFXXMLNode::GetNextSibling | SFXXMLNode::GetPreviousSibling


SFXXMLNode::GetPreviousSibling
Get the previous sibling node.
[ public, virtual, const ]
SFXXMLNodePtr GetPreviousSibling(Void);

Return value

Return null.

Reference

SFXXMLNode::GetNextSibling | SFXXMLNode::GetFirstChild | SFXXMLNode::GetLastChild | SFXXMLNode::GetChildNodes | SFXXMLNode::GetParentNode


SFXXMLNode::GetText
Get the node text.
[ public, const ]
SFXAnsiString GetText(Void);

Description

Return value differs depending on the node type.

Node type Return value
Attribute, Element, Entity, EntityReference, DocumentFragment concatenation of the textContent attribute value of every child node, excluding the Comment and ProcessingInstruction nodes. This is the empty string if the node has no children.
Text, CDATASection, Comment, ProcessingInstruction same as the return value of the GetNodeValue function(value of the nodeValue attribute )
Document, DocumentType, Notation null
[Note] Note
The node text does not contain any markup, and white spaces are not normalized.

Reference

SFXXMLNode::SetText


SFXXMLNode::HasChildNodes
Check whether this node has any child nodes or not.
[ public, virtual, const ]
Bool HasChildNodes(Void);

Return value

Retrun false.


SFXXMLNode::InsertBefore
Insert the specified node.
[ public, virtual ]
SFCError InsertBefore(
    SFXXMLNodeConstPtr newchild   // node to insert
    SFXXMLNodeConstPtr refchild   // reference node, before which the new node will be inserted
);

Return value

Return SFERR_UNSUPPORTED.

Description

Insert the newchild node before the refchild node. If the refchild argument is specified as null, append the newchild node as the last child node.

When the newchild node is the DocumentFragment node, all the child nodes of the newchild node are inserted in the same order before the refchild node.

When the newchild node has already existed in the DOM tree, it is removed before insertion.

Reference

SFXXMLNode::RemoveChild | SFXXMLNode::ReplaceChild | SFXXMLNode::CloneNode | SFXXMLNode::AppendChild


SFXXMLNode::IsSameNode
Check whether this node is the same as the specified one or not.
[ public, virtual, const ]
Bool IsSameNode(
    SFXXMLNodeConstPtr other   // node to compare with
);

Return value

  • If same: true
  • Otherwise: false

Description

Chech whether the two nodes refer to the same node or not.

Reference

SFXXMLNode::EqualNode


SFXXMLNode::RemoveChild
Remove the specified child node including its descendant nodes.
[ public, virtual ]
SFCError RemoveChild(
    SFXXMLNodeConstPtr oldchild   // child node to remove
);

Return value

Return SFERR_UNSUPPORTED.

Reference

SFXXMLNode::AppendChild | SFXXMLNode::ReplaceChild | SFXXMLNode::CloneNode | SFXXMLNode::InsertBefore


SFXXMLNode::ReplaceChild
Replace the specified child node.
[ public, virtual ]
SFCError ReplaceChild(
    SFXXMLNodeConstPtr newchild   // child node to replace with
    SFXXMLNodeConstPtr oldchild   // child node to be replaced
);

Return value

Return SFERR_UNSUPPORTED.

Reference

SFXXMLNode::AppendChild | SFXXMLNode::RemoveChild | SFXXMLNode::CloneNode | SFXXMLNode::InsertBefore


SFXXMLNode::SetNodeValue
Set the node value.
[ public, virtual ]
Void SetNodeValue(
    ACharConstPtr value   // node value
);

Reference

SFXXMLNode::GetNodeValue


SFXXMLNode::SetText
Set the node text.
[ public ]
SFCError SetText(
    SFXAnsiStringConstRef text   // node text to set
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If insufficient memory: SFERR_NO_MEMORY

Description

The behaviour of SFXXMLNode::SetText function differs depending on the node type.

Node type Description
Attribute, Element, Entity, EntityReference, DocumentFragment All child nodes are deleted. And then the Text node set with the specified node text is appended as a new child node.
Text, CDATASection, Comment, ProcessingInstruction Same as the SetNodeValue function. ( The specified node text is set to the nodeValue attribute of this node. )
Document, DocumentType, Notation Invalid. (Nothing happens.)

Reference

SFXXMLNode::GetText


SFXXMLNode::DOMNamedNodeMap
Class to manage the DOM nodes by name
[ public ]

SFMTYPEDEFCLASS(DOMNamedNodeMap)
class DOMNamedNodeMap {
    private:
        SFXXMLNodePtr           _ownerNode;
        DOMNodeList             _namenodelist;
    public:
        explicit                DOMNamedNodeMap         (Void);
                                ~DOMNamedNodeMap        (Void);
    public:        
        SInt32                  GetLength               (Void) const;
        SFXXMLNodePtr           Item                    (SInt32 index) const;
        SFXXMLNodePtr           GetNamedItem            (SFXAnsiStringConstRef name) const;
        SFCError                SetNamedItem            (SFXXMLNodeConstPtr arg);
        SFXXMLNodePtr           RemoveNamedItem         (SFXAnsiStringConstRef name);
        SFXXMLNodePtr           GetNamedItemNS          (SFXAnsiStringConstRef localname, SFXAnsiStringConstRef namespaceURI) const;
        SFCError                SetNamedItemNS          (SFXXMLNodeConstPtr arg);
        SFXXMLNodePtr           RemoveNamedItemNS       (SFXAnsiStringConstRef localname, SFXAnsiStringConstRef namespaceURI);
    protected:
        inline  Void            SetOwnerNode            (SFXXMLNodePtr node)
        {
             _ownerNode = node;
             return;
        }

    friend class SFXXMLElement;
    friend class SFXXMLDocumentType;
};

Description

The SFXXMLNode::DOMNamedNodeMap class for managing the DOM nodes provides the function to obtain a node by name(the nodeName attribute value).

Though no order relation exists among the nodes stored in the map(instance of SFXXMLNode::DOMNamedNodeMap class), a node can be obtained by index.

The following functions are available for the SFXXMLNode::DOMNamedNodeMap class.

Item Get the node specified by the index argument.
GetLength Get the number of nodes in the map.
Merge Merge the map specified by the nodemap argument.
GetNamedItem Get the node which has the name specified by the name argument. QName can be specified if namespace is set.
RemoveNamedItem Remove the node which has the name specified by the name argument. QName can be specified if namespace is set.
SetNamedItem Set the node which has the name specified by the name argument to the map. QName can be specified if namespace is set.
GetNamedItemNS Get the node which has the local name and the namespace URI specified by the localname and namespaceURI arguments.
RemoveNamedItemNS Remove the node which has the local name and the namespace URI specified by the localname and namespaceURI arguments.
SetNamedItemNS Add the specified node to the map. If the node with the same local name and namespace URI of the specified node already exists, it will be removed.

Example

// XML document to be parsed ("apple.xml")
<fruit>
    <name made="japan" date="2007-06-20" size="XS">apple</name>
</fruit>

Sample code using SFXXMLNode::DOMNamedNodeMap class.

SFCError error;             // error value
SFXXMLDOMParser parser;     // DOM parser
SFXAnsiString resultString; // variable to store node value of Attribute node

// parse "apple.xml" document 
if ((error = parser.Parse("/apple.xml")) == SFERR_NO_ERROR) {

    // get root of DOM tree
    SFXXMLDocumentPtr root = parser.GetDocument(); 

    // get root element of xml document(first child node of root of DOM tree. In this case, "fruit" element)
    SFXXMLNodePtr child = root->GetFirstChild(); 

    // get first child element of "fruit" element( "name" element )
    child = child->GetFirstChild(); 

    // get all Attribute nodes of "name" element
    SFXXMLNode::DOMNamedNodeMapPtr nodeMap = SFXXMLElementPtr(child)->GetAttributes();

    //  display number of Attribute nodes in map
    TRACE("%d",nodeMap->GetLength());   // "3" will be displayed

    //  display all attribute values of "name" element
    for (int i = 0; i < nodeMap->GetLength(); i++) {
 SFXXMLNodePtr node = nodeMap->Item(i); // get i-th item(Attribute node) from map by index
        resultString = node->GetNodeValue();   // get node value of i-th item(Attribute node)

        // display the obtained node value
        TRACE("%s",resultString.GetCString()); 
    }
    // "XS", "2007-06-20", and "japan" will be displayed
    
    // get node by name("date ")
    SFXXMLAttributePtr date = static_cast<SFXXMLAttributePtr>(nodeMap->GetNamedItem( "date "));
    resultString = date->GetNodeValue();     // get node value
	
    // display obtained node value
    TRACE("%s",resultString.GetCString());      // "2007-06-20" will be displayed
    
    // delete node specified by name("made")
    SFXXMLNodePtr remove = nodeMap->RemoveNamedItem("made");
    // delete node pointer
    ::delete remove;
	
    //  display all attribute values of "name" element
    for (int i = 0; i < nodeMap->GetLength(); i++) {
 SFXXMLNodePtr node = nodeMap->Item(i); // get i-th item(Attribute node) from map by index
        resultString = node->GetNodeValue();   // get node value of i-th item(Attribute node)

        // display obtained node value
        TRACE("%s",resultString.GetCString()); 
    }
    // "XS" and "2007-06-20" will be displayed
    
    // add specified node to map
    nodeMap->SetNamedItem(root->CreateAttribute("color", "red"));
    
    //  display number of nodes in map
    TRACE("%d",nodeMap->GetLength());   // 3
    
    //  display all attribute values of "name" element
    for (int i = 0; i < nodeMap->GetLength(); i++) {
 SFXXMLNodePtr node = nodeMap->Item(i); // get i-th item(Attribute node) from map by index
        resultString = node->GetNodeValue();   // get node value of i-th item(Attribute node)

        // display obtained node value
        TRACE("%s",resultString.GetCString()); 
    }
    // "XS", "2007-06-20", and "red" will be displayed
}

Reference

SFXXMLNode::DOMNodeList


SFXXMLNode::DOMNodeList
Class to manage the DOM nodes by list
SFMTYPEDEFALIAS(SFXList<SFXXMLNodePtr>, DOMNodeList)

Description

SFXXMLNode::DOMNodeList is the same as SFXList<SFXXMLNodePtr>. There is an order relation among the nodes of this node list.

Example

// XML document to be parsed ("apple.xml")
<fruit>
  <name>apple</name>
  <size>XS</size>
  <color>red</color>
</fruit>
SFCError error;             // error value
SFXXMLDOMParser parser;     // DOM parser
SFXAnsiString resultString; // variable to store node text

// parse "apple.xml" document 
if ((error = parser.Parse("/apple.xml")) == SFERR_NO_ERROR) {

    // get root of DOM tree
    SFXXMLDocumentPtr root = parser.GetDocument(); 

    // get root element of xml document(first child node of root of DOM tree. In this case, "fruit" element)
    SFXXMLNodePtr child = root->GetFirstChild();   

    // get all child elements of "fruit" element
    SFXXMLNode::DOMNodeListPtr nodeList = child->GetChildNodes();

    // get iterator
    SFXXMLNode::DOMNodeList::Iterator itor = nodeList->GetFirstIterator();   
    
    // display texts of all child elements of "fruit" element
    while (itor.HasNext()) { 
        // get "name" element, "size" element,"color" element sequentially
 SFXXMLNodePtr node = itor.GetNext();

        // get node name
        resultString = node->GetNodeName();

        // get node text
        resultString = node->GetText();
        // display node text(e.g. in case of "name" element, "apple")
        TRACE("%s",resultString.GetCString());
    }
}

Reference

SFXXMLNode::DOMNamedNodeMap


SFXXMLNode::NodeType
SFXXMLNode::NodeType is an enumeration type of NodeType values defined in the W3C DOM Level 1 Specification. Document Object Model (DOM) Level 1 Specification )
enum NodeType {
    ELEMENT_NODE                = 1,
    ATTRIBUTE_NODE              = 2,
    TEXT_NODE                   = 3,
    CDATA_SECTION_NODE          = 4,
    ENTITY_REFERENCE_NODE       = 5,
    ENTITY_NODE                 = 6,
    PROCESSING_INSTRUCTION_NODE = 7,
    COMMENT_NODE                = 8,
    DOCUMENT_NODE               = 9,
    DOCUMENT_TYPE_NODE          = 10,
    DOCUMENT_FRAGMENT_NODE      = 11,
    NOTATION_NODE               = 12,
    UNKNOWN_NODE                = -1
};