This article mainly introduces the tree structure in XML and the DOM Document Object Model. This article provides an example of JavaScript parsing DOM objects, for more information about the tree structure and DOM Document Object Model in XML, see the next article. This article provides an example of using JavaScript to parse DOM objects. For more information, see
Tree structure
XML documents are always descriptive. A tree structure is usually called an XML tree. it plays an important role in describing XML documents.
This tree structure contains root (parent) elements, child elements, and so on. By using the tree structure, we can understand all subsequent branches and subbranches originating from the root element. The parsing starts from the root element, and then moves down to the first branch pointing to an element. from here, the first branch and its subnodes are processed.
Example
The following example demonstrates a simple XML tree structure:
Tanmay
Patil
1234567890
tanmaypatil@xyz.com
Bangalore
Karnataka
560212
The following tree structure indicates the preceding XML document:
In the figure, there is . There is another Element. There are five more branches in the employee element: , , , And. There are three sub-branches in the element, which are , And .
DOM Document Object Model
The Document Object Model (DOM) is the basis of XML. XML documents have an information hierarchy unit called nodes. DOM is a way to describe the relationship between these nodes and them.
A DOM document is a collection of nodes or information blocks organized by hierarchies. This hierarchy allows developers to navigate to this node tree to query specific information. Because it is based on the information hierarchy, DOM is also considered to be _ based on the node tree.
On the other hand, xml dom also provides an API that allows developers to add, edit, move, or remove nodes anywhere in the node tree to create applications.
Example
The example (sample.htm) below parses an XML document ("address. xml") into an xml dom object and extracts some information using JavaScript:
TutorialsPoint DOM example Name:
Company:
Phone:
《script》 if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","/xml/address.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; document.getElementById("name").innerHTML= xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue; document.getElementById("company").innerHTML= xmlDoc.getElementsByTagName("company")[0].childNodes[0].nodeValue; document.getElementById("phone").innerHTML= xmlDoc.getElementsByTagName("phone")[0].childNodes[0].nodeValue; 《script》
The content of address. xml is as follows:
Tanmay Patil
TutorialsPoint
(011) 123-4567
We can save the two sample.htm and address. xml files to the same directory/xml, and then execute the sample.htm file in a browser. It should generate the following results:
Here, we can see that each subnode is extracted and their values are displayed.
The above is the detailed content of the tree structure in XML and the sample code (figure) of the DOM Document Object Model. For more information, see other related articles in the first PHP community!