Navigation methods usage examples

Examples using the navigation methods of the xml.DomDocument class.

DomDocument navigation functions deal with nodes immediately under the DomDocument object, except for search features. To navigate through all the nodes, you can refer to the navigation functions of the class xml.DomNode.
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="card.xsl"?>
<!-- demo card -->
<CardList xml:id="1" >[...]
</CardList>

In the example the first node of the document is xml-stylesheet. Use getFirstDocumentNode to get the node. The element at position 2 is the comment <!-- demo card -->. Use getDocumentNodeItem function to get the node.

The last node of the document is CardList. Use getLastDocumentNode to get the node.

The number of nodes in the document is 3. This is the result of the function getDocumentNodesCount. This function only counts the number of children immediately under the DomDocument.

Note:

The first line of the example, <?xml version="1.0" encoding="ISO-8859-1"?>, is not considered as a node. To access the information of the first line, use getXmlVersion() and getXmlEncoding functions.

Caution, if the example is in pretty printed format, the results are not the same. There are additional text nodes representing the carriage returns.
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="card.xsl"?>
<!-- demo card -->
<CardList xml:id="1" > [...]
</CardList>
See Cautions section for more details.

You can select nodes using their tag names, by XPath, or by their attributes value (if of type ID, xml:id for example). The getElementsbyTagName and getElementsbyTagNameNS methods return a DomNodeList object, unlike the other methods that return a DomNode object. The DomNodeList is restricted to containing objects with the same tag name and/or namespace. The selectByXPath method also returns a DomNodeList object, but each node can have a different name.

getElementsByTagNameNS("message","http://schemas.xmlsoap.org/wsdl/")
Get the message nodes that have http://schemas.xmlsoap.org/wsdl/ as the namespace.
getElementsByTagNameNS("message","*")
Get all the message nodes, regardless of the namespace they have.
getElementsByTagName("message")
Get all the message nodes that do not have any namespace.
selectByXPath("//xs:element",NULL)
Get all the xs:element nodes that have a namespace corresponding to the prefix xs .
selectByXPath("//Card",NULL)
Get all the Card nodes that do not have any namespace.
getElementById("1")
Get the unique node whose attribute of type ID has a value of "1".