Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM Examples

XML DOM Examples


XML DOM Parsing

The following example uses the XML file books.xml.

Load an XML File
Load an XML String

Example Explanation


XML DOM Properties and Methods

The following example uses the XML file books.xml.
function loadXMLDoc(), in external JavaScript, used to load an XML file.
function loadXMLString(), in external JavaScript, used to load an XML string.

Load and parse an XML file - External parsing script
Load and parse an XML string - External parsing script

Example Explanation


XML DOM Accessing Nodes

The following example uses the XML file books.xml.
function loadXMLDoc(), in external JavaScript, used to load an XML file.

Access nodes using index numbers in a node list
Loop through nodes using the length property
Check the node type of an element
Loop through element nodes
Loop through element nodes using node relationships

Example Explanation


XML DOM Node Information

The following example uses the XML file books.xml.
function loadXMLDoc(), in external JavaScript, used to load an XML file.

Get an Element Node's Node Name
Get Text from a Text Node
Change the text of a text node
Get the node name and type of an element node

Example Explanation


XML DOM Node Lists and Attribute Lists

The following example uses the XML file books.xml.
function loadXMLDoc(), in external JavaScript, used to load an XML file.

From the first get the text of an element</a><br /> <a href="/try/try.php?filename=try_dom_nodelist_length">Use the length property to loop through the nodes</a><br /> <a href="/try/try.php?filename=try_dom_attributes">Get the attributes of an element</a></p> <p><a href="dom-nodes-nodelist.html">Example Explained</a></p> <hr /> <p><strong>XML DOM Traverse the Node Tree</strong></p> <p>The following example uses the XML file <a href="/try/demo_source/books.xml">books.xml</a>.<br /> Function <a href="dom-loadxmldoc.html">loadXMLString()</a>, in external JavaScript, used to load an XML string.</p> <p><a href="/try/try.php?filename=try_dom_parsertest3">Traverse the node tree</a></p> <p><a href="dom-nodes-traverse.html">Example Explained</a></p> <hr /> <p><strong>XML DOM Browser Differences</strong></p> <p>The following example uses the XML file <a href="/try/demo_source/books.xml">books.xml</a>.<br /> Function <a href="dom-loadxmldoc.html">loadXMLDoc()</a>, in external JavaScript, used to load an XML file.</p> <p><a href="/try/try.php?filename=try_dom_nodetype_ievsmozilla">Display the length of the node list - different results in IE and other browsers</a><br /> <a href="/try/try.php?filename=try_dom_nodetype_ievsmozilla2">ignore empty text between nodes</a></p> <p><a href="dom-mozilla-vs-ie.html">Example Explained</a></p> <hr /> <p><strong>XML DOM Navigate Nodes</strong></p> <p>The following example uses the XML file <a href="/try/demo_source/books.xml">books.xml</a>.<br /> Function <a href="dom-loadxmldoc.html">loadXMLDoc()</a>, in external JavaScript, used to load an XML file.</p> <p><a href="/try/try.php?filename=try_dom_nav_parentnode">Get the parent node of a node</a><br /> <a href="/try/try.php?filename=try_dom_nav_firstchild">Get the first child element of a node</a><br /> <a href="/try/try.php?filename=try_dom_nav_lastchild">Get the last child element of a node</a><br /> <a href="/try/try.php?filename=try_dom_nav_nextsibling">Get the next sibling element of a node</a><br /> <a href="/try/try.php?filename=try_dom_nav_prevsibling">Get the previous sibling element of a node</a></p> <p><a href="dom-nodes-navigate.html">Example Explained</a></p> <hr /> <p><strong>XML DOM Get a Node Value</strong></p> <p>The following example uses the XML file <a href="/try/demo_source/books.xml">books.xml</a>.<br /> Function <a href="dom-loadxmldoc.html">loadXMLDoc()</a>, in external JavaScript, used to load an XML file.</p> <p><a href="/try/try.php?filename=try_dom_getelementsbytagname">Get the text value of an element</a><br /> <a href="/try/try.php?filename=try_dom_getattribute">Get the text value of an attribute</a></p> <p><a href="dom-nodes-get.html">Example Explained</a></p> <hr /> <p><strong>XML DOM Change a Node Value</strong></p> <p>The following example uses the XML file <a href="/try/demo_source/books.xml">books.xml</a>.<br /> Function <a href="dom-loadxmldoc.html">loadXMLDoc()</a>, in external JavaScript, used to load an XML file.</p> <p><a href="/try/try.php?filename=try_dom_change_nodevalue">Change an element's text node</a><br /> <a href="/try/try.php?filename=try_dom_setattribute1">Use setAttribute to change an attribute value</a><br /> <a href="/try/try.php?filename=try_dom_att_nodevalue">Use nodeValue to change an attribute value</a></p> <p><a href="dom-nodes-set.html">Example Explained</a></p> <hr /> <p><strong>XML DOM Remove a Node</strong></p> <p>The following example uses the XML file <a href="/try/demo_source/books.xml">books.xml</a>.<br /> Function <a href="dom-loadxmldoc.html">loadXMLDoc()</a>, in external JavaScript, used to load an XML file.</p> <p><a href="/try/try.php?filename=try_dom_removechild">Delete an element node</a><br /> <a href="/try/try.php?filename=try_dom_removecurrent">Delete the current element node</a><br /> <a href="/try/try.php?filename=try_dom_removetextnode">Delete a text node</a><br /> <a href="/try/try.php?filename=try_dom_remove_nodevalue">Clear the text of the text node</a><br /> <a href="/try/try.php?filename=try_dom_removeattribute">Remove an attribute by name</a><br /> <a href="/try/try.php?filename=try_dom_removeattributenode">Remove an attribute by object</a></p> <p><a href="dom-nodes-remove.html">Example Explained</a></p> <hr /> <p><strong>XML DOM Replace a Node</strong></p> <p>The following example uses the XML file <a href="/try/demo_source/books.xml">books.xml</a>.<br /> Function <a href="dom-loadxmldoc.html">loadXMLDoc()</a>, in external JavaScript, used to load an XML file.</p> <p><a href="/try/try.php?filename=try_dom_replacechild">Replace an element node</a><br /> <a href="/try/try.php?filename=try_dom_replacedata2">Replace the data in a text node</a></p> <p><a href="dom-nodes-replace.html">Example Explained</a></p> <hr /> <p><strong>XML DOM Create a Node</strong></p> <p>The following example uses the XML file <a href="/try/demo_source/books.xml">books.xml</a>.<br /> Function <a href="dom-loadxmldoc.html">loadXMLDoc()</a>, in external JavaScript, used to load an XML file.</p> <p><a href="/try/try.php?filename=try_dom_createelement1">Create an element node</a><br /> <a href="/try/try.php?filename=try_dom_createattribute">Use createAttribute to create an attribute node</a><br /> <a href="/try/try.php?filename=try_dom_createattribute3">Use setAttribute to create an attribute node</a><br /> <a href="/try/try.php?filename=try_dom_createelement1">Create a text node</a><br /> <a href="/try/try.php?filename=try_dom_createcdatasection1">Create a CDATA section node</a><br /> <a href="/try/try.php?filename=try_dom_createcomment1">Create a comment node</a></p> <p><a href="dom-nodes-create.html">Example Explained</a></p> <hr /> <p><strong>XML DOM Add a Node</strong></p> <p>The following example uses the XML file <a href="/try/demo_source/books.xml">books.xml</a>.<br /> Function <a href="dom-loadxmldoc.html">loadXMLDoc()</a>, in external JavaScript, used to load an XML file.</p> <p><a href="/try/try.php?filename=try_dom_createelement2">Add a node after the last child node</a><br /> <a href="/try/try.php?filename=try_dom_insertbefore">Add a node before the specified child node</a><br /> <a href="/try/try.php?filename=try_dom_createattribute3">Add a new attribute</a><br /> <a href="/try/try.php?filename=try_dom_insertdata">Add data to a text node</a></p> <p><a href="dom-nodes-add.html">Example Explained</a></p> <hr /> <p><strong>XML DOM Clone a Node</strong></p> <p>The following example uses the XML file <a href="/try/demo_source/books.xml">books.xml</a>.
function
loadXMLDoc(), in external JavaScript, used to load an XML file.

Copy a node and append it to an existing node

Example Explanation


XML DOM XMLHttpRequest Object

A simple XMLHttpRequest example
Create a simple XMLHttpRequest and retrieve data from a TXT file.

Retrieve header information with getAllResponseHeaders()
Retrieve the header information of a resource (file).

Retrieve specified header information with getResponseHeader()
Retrieve a specified header of a resource (file).

Retrieve the content of an ASP file
How a web page communicates with a web server when a user types characters into an input field.

Retrieve content from a database
How a web page extracts information from a database through the XMLHttpRequest object.

Retrieve the content of an XML file
Create an XMLHttpRequest to retrieve data from an XML file and display the data in an HTML table.

Example Explanation