Home > Backend Development > XML/RSS Tutorial > Crazy XML study notes (5) -----------XML DOM

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

黄舟
Release: 2017-02-21 14:30:34
Original
1392 people have browsed it


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

xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue
Copy after login

  • xmlDoc - XML ​​document created by the parser

  • getElementsByTagName("to")[0] - First element

  • childNodes[0] - The first child element of the 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=
Copy after login

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

Copy after login
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
Copy after login
In this XML example, there is only one 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 nodes.

Parsing XML String - Cross-Browser Example

The code below loads and Parse an XML string:





W3School.com.cn Internal Note

To:
From:
Message:

Copy after login
Output:

To: GeorgeFrom: JohnMessage: Don't forget the meeting!
Copy after login

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 (m.sbmmt.com) for more related content!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template