SophiaFramework UNIVERSE 5.3 |
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 | |
---|---|
The values of nodeType, nodeName, nodeValue attributes can be obtained by the SFXXMLNode::GetNodeType, SFXXMLNode::GetNodeName, SFXXMLNode::GetNodeValue functions respectively. |
Note | |
---|---|
The attributes is valid only for the Element node, and can be obtained by the SFXXMLElement::GetAttributes function. |
Document Object Model (DOM) Specification | |
---|---|
W3C DOM Level 2 Specification: Document Object Model (DOM) Level 2 Core Specification |
SFXXMLAttribute | SFXXMLCDATASection | SFXXMLComment | SFXXMLDocument | SFXXMLDocumentFragment | SFXXMLDocumentType | SFXXMLElement | SFXXMLEntity | SFXXMLEntityReference | SFXXMLNode | SFXXMLNotation | SFXXMLProcessingInstruction | SFXXMLText
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 )
|
[ public, explicit ] SFXXMLNode( SFXXMLDocumentPtr owner // the Document node that owns this node );
[ protected, explicit ] SFXXMLNode(Void);
[ public, virtual ] virtual ~SFXXMLNode(Void);
[ public, virtual ] SFCError AppendChild( SFXXMLNodeConstPtr newchild // child node to append );
Return SFERR_UNSUPPORTED.
SFXXMLNode::RemoveChild | SFXXMLNode::ReplaceChild | SFXXMLNode::CloneNode | SFXXMLNode::InsertBefore
[ public, virtual ] SFCError CloneNode( SFXXMLNodeHandle clonenode // destination node(Handle type) Bool deep // whether or not to copy the child nodes recursively );
Return SFERR_UNSUPPORTED.
SFXXMLNode::RemoveChild | SFXXMLNode::ReplaceChild | SFXXMLNode::AppendChild | SFXXMLNode::InsertBefore
[ public, virtual, const ] Bool EqualNode( SFXXMLNodeConstPtr arg // specified node );
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.
[ public, virtual, const ] DOMNodeListPtr GetChildNodes(Void);
Return null.
Get a pointer to the SFXXMLNode::DOMNodeList instance that contains all child nodes of this node.
Note | |
---|---|
Each element of SFXXMLNode::DOMNodeList is of the "pointer to the SFXXMLNode" type. |
SFXXMLNode::DOMNodeList | SFXXMLNode::GetFirstChild | SFXXMLNode::GetLastChild | SFXXMLNode::GetNextSibling | SFXXMLNode::GetParentNode | SFXXMLNode::GetPreviousSibling
[ public, virtual, const ] SFXXMLNodePtr GetFirstChild(Void);
Return null.
SFXXMLNode::GetLastChild | SFXXMLNode::GetChildNodes | SFXXMLNode::GetNextSibling | SFXXMLNode::GetPreviousSibling | SFXXMLNode::GetParentNode
[ public, virtual, const ] SFXXMLNodePtr GetLastChild(Void);
Return null.
SFXXMLNode::GetFirstChild | SFXXMLNode::GetChildNodes | SFXXMLNode::GetNextSibling | SFXXMLNode::GetPreviousSibling | SFXXMLNode::GetParentNode
[ public, virtual, const ] SFXXMLNodePtr GetNextSibling(Void);
Return null.
SFXXMLNode::GetPreviousSibling | SFXXMLNode::GetFirstChild | SFXXMLNode::GetLastChild | SFXXMLNode::GetChildNodes | SFXXMLNode::GetParentNode
[ public, virtual, const ] ACharConstPtr GetNodeName(Void);
Return null.
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. |
[ public, virtual, const ] NodeType GetNodeType(Void);
Return UNKNOWN_NODE.
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.
|
[ public, virtual, const ] ACharConstPtr GetNodeValue(Void);
Return null.
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. |
[ public, virtual, const ] SFXXMLNodePtr GetParentNode(Void);
Return null.
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.
SFXXMLNode::GetFirstChild | SFXXMLNode::GetLastChild | SFXXMLNode::GetChildNodes | SFXXMLNode::GetNextSibling | SFXXMLNode::GetPreviousSibling
[ public, virtual, const ] SFXXMLNodePtr GetPreviousSibling(Void);
Return null.
SFXXMLNode::GetNextSibling | SFXXMLNode::GetFirstChild | SFXXMLNode::GetLastChild | SFXXMLNode::GetChildNodes | SFXXMLNode::GetParentNode
[ public, const ] SFXAnsiString GetText(Void);
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 | |
---|---|
The node text does not contain any markup, and white spaces are not normalized. |
[ public, virtual, const ] Bool HasChildNodes(Void);
Retrun false.
[ public, virtual ] SFCError InsertBefore( SFXXMLNodeConstPtr newchild // node to insert SFXXMLNodeConstPtr refchild // reference node, before which the new node will be inserted );
Return SFERR_UNSUPPORTED.
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.
SFXXMLNode::RemoveChild | SFXXMLNode::ReplaceChild | SFXXMLNode::CloneNode | SFXXMLNode::AppendChild
[ public, virtual, const ] Bool IsSameNode( SFXXMLNodeConstPtr other // node to compare with );
Chech whether the two nodes refer to the same node or not.
[ public, virtual ] SFCError RemoveChild( SFXXMLNodeConstPtr oldchild // child node to remove );
Return SFERR_UNSUPPORTED.
SFXXMLNode::AppendChild | SFXXMLNode::ReplaceChild | SFXXMLNode::CloneNode | SFXXMLNode::InsertBefore
[ public, virtual ] SFCError ReplaceChild( SFXXMLNodeConstPtr newchild // child node to replace with SFXXMLNodeConstPtr oldchild // child node to be replaced );
Return SFERR_UNSUPPORTED.
SFXXMLNode::AppendChild | SFXXMLNode::RemoveChild | SFXXMLNode::CloneNode | SFXXMLNode::InsertBefore
[ public, virtual ] Void SetNodeValue( ACharConstPtr value // node value );
[ public ] SFCError SetText( SFXAnsiStringConstRef text // node text to set );
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.) |
[ 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; };
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. |
// 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 }
SFMTYPEDEFALIAS(SFXList<SFXXMLNodePtr>, DOMNodeList)
SFXXMLNode::DOMNodeList is the same as SFXList<SFXXMLNodePtr>. There is an order relation among the nodes of this node list.
// 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()); } }
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 };
Copyright(c) 2002 - 2024 Sophia Cradle Incorporated All Rights Reserved. |