XML DOM instance

XML DOM parsing

The XML file used in the following example:books.xml

Load and parse an XML file
Load and parse an XML string

Example explanation

XML DOM attributes and methods

The following example uses an XML file books.xml.

Function loadXMLDoc(), located in external JavaScript, used to load XML files.

Function loadXMLString(), located in external JavaScript, used to load an XML string.

Load and parse an XML file
Load and parse an XML string

Example explanation

XML DOM Node Information

The following example uses an XML file books.xml.

Function loadXMLDoc(), located in external JavaScript, used to load XML files.

Get the node name of the element node
Get text from the text node
Change the text in the text node
Get the node name and type of the element node

Example explanation

XML DOM node list and attribute list

The following example uses an XML file books.xml.

Function loadXMLDoc(), located in external JavaScript, used to load XML files.

Get text from the first <title> element
Loop through nodes using the length property
Get the attributes of the element

Example explanation

XML DOM traverse the node tree

The following example uses an XML file books.xml.

Function loadXMLString(), located in external JavaScript, used to load XML files.

Traverse a node tree
Loop through all child nodes of the <book> element.

Example explanation

XML DOM Browser Differences

The following example uses an XML file books.xml.

Function loadXMLDoc(), located in external JavaScript, used to load XML files.

Display the length of the node list
This example displays the length of a node list. The result is different in IE and other browsers.
Ignore the empty text between nodes
This example checks the nodeType of the node and only handles element nodes.

Example explanation

XML DOM Get Node Value

The following example uses an XML file books.xml.

Function loadXMLDoc(), located in external JavaScript, used to load XML files.

Get the element value
Get the attribute value

Example explanation

XML DOM Change Node Value

The following example uses an XML file books.xml.

Function loadXMLDoc(), located in external JavaScript, used to load XML files.

Change the text node of the element
Change the attribute value by using setAttribute
Change the attribute value by using nodeValue

Example explanation

XML DOM Replace Node

The following example uses an XML file books.xml.

Function loadXMLDoc(), located in external JavaScript, used to load XML files.

Replace the element node
Replace the data in the text node

Example explanation

XML DOM Add Node

The following example uses an XML file books.xml.

Function loadXMLDoc(), located in external JavaScript, used to load XML files.

Add a node after the last child node
Add a node before the specified child node
Add a new attribute
Add data to a text node

Example explanation

XML DOM Clone Node

The following example uses an XML file books.xml.

Function loadXMLDoc(), located in external JavaScript, used to load XML files.

Copy a node and append it to an existing node
This example uses cloneNode() to copy a node and append it to the root node of the XML document.

Example explanation