Advertisement

XML DOM Examples

XML DOM parsing

XML files used in the examples below: the Books.xml

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

Example Explained

XML DOM Properties and Methods

The following example uses XML files the Books.xml .

Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

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

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

Example Explained

XML DOM node information

The following example uses XML files the Books.xml .

Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

Get the name of the node element node
Get text from text node
Change text node text
Gets the element node with name and type

Example Explained

XML DOM node lists and property lists

The following example uses XML files the Books.xml .

Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

Get the text from the first <title> element
By using the length property to loop node
Get element attributes

Example Explained

XML DOM node tree traversal

The following example uses XML files the Books.xml .

Function loadXMLString () , in an external JavaScript is used to load the XML file.

Traversing the tree of nodes
Cycle <book> elements of all child nodes.

Example Explained

XML DOM Browser Differences

The following example uses XML files the Books.xml .

Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

The length of the display node list
This example shows the length of a node list. In IE and other browsers, the result is different.
Ignore empty text nodes
nodeType check node in this case, and only the processing element nodes.

Example Explained

XML DOM node localization

The following example uses XML files the Books.xml .

Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

Being the parent of a node
Being the first child of node
Get the last child node
Get the next sibling element node
Get on a sibling element node

Example Explained

Get XML DOM node value

The following example uses XML files the Books.xml .

Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

Gets the value of the element
Gets the value of the property

Example Explained

XML DOM node values ​​change

The following example uses XML files the Books.xml .

Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

Change the text of an element node
By using setAttribute to change the value of the property
By using nodeValue to change the property value

Example Explained

XML DOM delete nodes

The following example uses XML files the Books.xml .

Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

Remove the element node
Remove the current element node
Delete text node
Clear text node
Remove attribute by name
Delete attributes according to the object

Example Explained

XML DOM replacement node

The following example uses XML files the Books.xml .

Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

Replace element node
Data replacement text node

Example Explained

Adding XML DOM node

The following example uses XML files the Books.xml .

Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

After the last child node to add a node
Before specified child node to add a node
Adding a new property
Add data to a text node

Example Explained

XML DOM node cloning

The following example uses XML files the Books.xml .

Function loadXMLDoc () , in an external JavaScript is used to load the XML file.

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 Explained