The Document Object Modeling (DOM) classes / The DomDocument class |
Examples using the navigation methods of the xml.DomDocument class.
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="card.xsl"?> <!-- demo card --> <CardList xml:id="1" >[...] </CardList>
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 node of the document is 3. This is result of function getDocumentNodeCount. This function only count the number of children immediately under the DomDocument.
Note that the first line of the example, <?xml version="1.0" encoding="ISO-8859-1"?>, is not considered as a node. To access to the information of the first line, use getXmlVersion() and getXmlEncoding functions.
<?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 contain objects with the same tag name and/or namespace. TheselectByXPath 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 has a namespace corresponding to 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".