Home > Article > Backend Development > Crazy XML study notes (5) -----------XML DOM
DOM (Document Object Model, Document Object Model) defines a set of standard methods for accessing and manipulating documents.
##XML DOM (XML Document Object Model) Definition A set of standard methods for accessing and manipulating XML documents.
DOM View XML documents as a tree structure. All elements can be accessed through the DOM tree. Their contents can be modified or deleted, and new elements created. Elements, their text, and their attributes are all considered nodes.
In the following example, we use a DOM reference to get the text from the 05034471df6398a62d5708f78b78e0ac element:
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue
xmlDoc - XML document created by the parser
getElementsByTagName("to")[0] - First05034471df6398a62d5708f78b78e0ac element
childNodes[0] - The first child element of the 05034471df6398a62d5708f78b78e0ac element (text node)
nodeValue - the value of the node (the text itself)
All HTML elements can be accessed through the HTML DOM.
In the following example, we use a DOM reference to change the text of the HTML element with id="to":
document.getElementById("to").innerHTML=
document - HTML document
- where id=" to" HTML element
- The inner text of the HTML element
Parse XML file - cross-browser example
The following code Load an XML document ("note.xml") into the XML parser:
Output:W3School.com.cn Internal Note
To:
From:
Message:
To:
GeorgeFrom: JohnMessage: Don't forget the meeting!
Important Note
To extract the text "John" from XML, the syntax is:
getElementsByTagName("from")[0].childNodes[0].nodeValueIn this XML example, there is only one 66fd2ada9ebb04d4250c850dc1e3737e tag, but you still need to specify the bottom of the array Tag [0] because the XML parser method getElementsByTagName() returns an array of all 66fd2ada9ebb04d4250c850dc1e3737e nodes.
Parsing XML String - Cross-Browser Example
The code below loads and Parse an XML string:
Output:W3School.com.cn Internal Note
To:
From:
Message:
To: GeorgeFrom: JohnMessage: Don't forget the meeting!
Note: Internet Explorer uses the loadXML() method to To parse XML strings, other browsers use DOMParser objects.
The above are the crazy XML study notes (5)-------- ---XML DOM content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!