Home  >  Article  >  Backend Development  >  Crazy XML study notes (5) -----------XML DOM

Crazy XML study notes (5) -----------XML DOM

黄舟
黄舟Original
2017-02-21 14:30:341350browse


DOM (Document Object Model, Document Object Model) defines a set of standard methods for accessing and manipulating documents.

XML DOM

##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)

HTML DOM

##HTML DOM (HTML Document Object Model) Definition A set of standard methods for accessing and manipulating HTML documents.

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

  • ##getElementById("to")

    - where id=" to" HTML element

  • innerHTML

    - 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:







W3School.com.cn Internal Note

To:
From:
Message:

Output:

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].nodeValue
In 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:







W3School.com.cn Internal Note

To:
From:
Message:

Output:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn