XML DOM Instance

XML DOM parsing

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

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

Example Explanation

XML DOM properties 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 by using the length property
Get the attributes of the element

Example Explanation

XML DOM Traversing 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.

Show the length of the node list
This example shows 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 node's nodeType and only processes element nodes.

Example Explanation

XML DOM Getting Node Values

The following example uses an XML file books.xml.

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

Get the value of an element
Get the value of an attribute

Example Explanation

XML DOM Changing Node Values

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 an element
Change the value of an attribute by using setAttribute
Change the attribute value by using nodeValue

Example Explanation

XML DOM Replacing Nodes

The following example uses an XML file books.xml.

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

Replace an element node
Replace the data in a text node

Example Explanation

XML DOM Adding Nodes

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 Cloning Nodes

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