The Difference Between XML and HTML
The Difference Between XML and HTML
XML is used in many aspects of web development, often to simplify data storage and sharing.
If you need to display dynamic data in your HTML document, it will take a lot of work to edit
the HTML each time the data changes.
With XML, data can be stored in separate XML files. This way you can concentrate on using
HTML for layout and display, and be sure that changes in the underlying data will not require
any changes to the HTML.
With a few lines of JavaScript, you can read an external XML file and update the data content of
your HTML.
In the real world, computer systems and databases contain data in incompatible formats.
XML data is stored in plain text format. This provides a software- and hardware-independent
way of storing data.
This makes it much easier to create data that different applications can share.
One of the most time-consuming challenges for developers is to exchange data between
incompatible systems over the Internet.
Exchanging data as XML greatly reduces this complexity, since the data can be read by different
incompatible applications.
Upgrading to new systems (hardware or software platforms), is always very time consuming.
Large amounts of data must be converted and incompatible data is often lost.
XML data is stored in text format. This makes it easier to expand or upgrade to new operating
systems, new applications, or new browsers, without losing data.
Since XML is independent of hardware, software and application, XML can make your data
more available and useful.
Different applications can access your data, not only in HTML pages, but also from XML data
sources.
With XML, your data can be available to all kinds of "reading machines" (Handheld computers,
voice machines, news feeds, etc), and make it more available for blind people, or people with
other disabilities.
XML Tree
XML documents form a tree structure that starts at "the root" and branches to "the leaves".
The first line is the XML declaration. It defines the XML version (1.0) and the encoding used
(ISO-8859-1 = Latin-1/West European character set).
The next line describes the root element of the document (like saying: "this document is a
note"):
<note>
The next 4 lines describe 4 child elements of the root (to, from, heading, and body):
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
And finally the last line defines the end of the root element:
</note>
You can assume, from this example, that the XML document contains a note to Tove from Jani.
Don't you agree that XML is pretty self-descriptive?
XML documents must contain a root element. This element is "the parent" of all other elements.
The elements in an XML document form a document tree. The tree starts at
the root and branches to the lowest level of the tree.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
The terms parent, child, and sibling are used to describe the relationships between elements.
Parent elements have children. Children on the same level are called siblings (brothers or sisters).
All elements can have text content and attributes (just like in HTML).
Example:
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurent is</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
The root element in the example is <bookstore>. All <book> elements in the document are
contained within <bookstore>.
The syntax rules of XML are very simple and logical. The rules are easy to learn, and easy to
use.
In HTML, you will often see elements that don't have a closing tag:
<p>This is a paragraph
<p>This is another paragraph
In XML, it is illegal to omit the closing tag. All elements must have a closing tag:
<p>This is a paragraph</p>
<p>This is another paragraph</p>
Note: You might have noticed from the previous example that the XML declaration did not have
a closing tag. This is not an error. The declaration is not a part of the XML document itself, and it
has no closing tag.
XML tags are case sensitive. With XML, the tag <Letter> is different from the tag <letter>.
Opening and closing tags must be written with the same case:
<Message>This is incorrect</message>
<message>This is correct</message>
Note: "Opening and closing tags" are often referred to as "Start and end tags". Use whatever you
prefer. It is exactly the same thing.
In the example above, "Properly nested" simply means that since the <i> element is opened
inside the <b> element, it must be closed inside the <b> element.
XML documents must contain one element that is the parent of all other elements. This element
is called the root element.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
XML elements can have attributes in name/value pairs just like in HTML.
In XML the attribute value must always be quoted. Study the two XML documents below. The
first one is incorrect, the second is correct:
<note date=12/11/2007>
<to>Tove</to>
<from>Jani</from>
</note>
<note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>
The error in the first document is that the date attribute in the note element is not quoted.
Entity References
If you place a character like "<" inside an XML element, it will generate an error because the
parser interprets it as the start of a new element.
To avoid this error, replace the "<" character with an entity reference:
Note: Only the characters "<" and "&" are strictly illegal in XML. The greater than character is
legal, but it is a good habit to replace it.
Comments in XML
XML Elements
An XML element is everything from (including) the element's start tag to (including) the
element's end tag.
An element can contain other elements, simple text or a mixture of both. Elements can also have
attributes.
<bookstore>
<book category="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
In the example above, <bookstore> and <book> have element contents, because they contain
other elements. <author> has text content because it contains text.
Names should be short and simple, like this: <book_title> not like this:
<the_title_of_the_book>.
Avoid "-" characters. If you name something "first-name," some software may think you want to
subtract name from first.
Avoid "." characters. If you name something "first.name," some software may think that "name"
is a property of the object "first."
Avoid ":" characters. Colons are reserved to be used for something called namespaces (more
later).
XML documents often have a corresponding database. A good practice is to use the naming rules
of your database for the elements in the XML documents.
<note>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>
Let's imagine that we created an application that extracted the <to>, <from>, and <body>
elements from the XML document to produce this output:
MESSAGE
To: Tove
From: Jani
Imagine that the author of the XML document added some extra information to it:
<note>
<date>2008-01-10</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
No. The application should still be able to find the <to>, <from>, and <body> elements in the
XML document and produce the same output.
One of the beauties of XML, is that it can often be extended without breaking applications.
XML Attributes
XML elements can have attributes in the start tag, just like HTML.
XML Attributes
From HTML you will remember this: <img src="computer.gif">. The "src" attribute provides
additional information about the <img> element.
<img src="computer.gif">
<a href="demo.asp">
Attributes often provide information that is not a part of the data. In the example below, the file
type is irrelevant to the data, but important to the software that wants to manipulate the element:
<file type="gif">computer.gif</file>
<person sex="female">
or like this:
<person sex='female'>
If the attribute value itself contains double quotes you can use single quotes, like in this
example:
<person sex="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
<person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
In the first example sex is an attribute. In the last, sex is an element. Both examples provide the
same information.
There are no rules about when to use attributes and when to use elements. Attributes are handy in
HTML. In XML my advice is to avoid them. Use elements instead.
My Favorite Way
The following three XML documents contain exactly the same information:
<note date="10/01/2008">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note>
<date>10/01/2008</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note>
<date>
<day>10</day>
<month>01</month>
<year>2008</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Sometimes ID references are assigned to elements. These IDs can be used to identify XML
elements in much the same way as the ID attribute in HTML. This example demonstrates this:
<messages>
<note id="501">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note id="502">
<to>Jani</to>
<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not</body>
</note>
</messages>
XML Validation
A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules
of a Document Type Definition (DTD):
The DOCTYPE declaration in the example above, is a reference to an external DTD file. The
content of the file is shown in the paragraph below.
XML DTD
The purpose of a DTD is to define the structure of an XML document. It defines the structure
with a list of legal elements:
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
If you want to study DTD, you will find our DTD tutorial on our homepage.
XML Schema
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
XML Validator
The W3C XML specification states that a program should stop processing an XML document if
it finds an error. The reason is that XML software should be small, fast, and compatible.
HTML browsers will display documents with errors (like missing end tags). HTML browsers are
big and incompatible because they have a lot of unnecessary code to deal with (and display)
HTML errors.
Paste your XML into the text area below, and syntax-check it by clicking the "Validate" button.
Note: This only checks if your XML is "Well formed". If you want to validate your XML against
a DTD, see the last paragraph on this page.
You can syntax-check an XML file by typing the URL of the file into the input field below, and
then click the "Validate" button:
Filename:
Note: If you get an "Access denied" error, it's because your browser security does not allow file
access across domains.
The file "note_error.xml" demonstrates your browsers error handling. If you want see an error
free message, substitute the "note_error.xml" with "cd_catalog.xml".
If you know DTD, you can validate your XML in the text area below.
Just add the DOCTYPE declaration to your XML and click the "Validate" button:
- <note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
</note>
The XML document will be displayed with color-coded root and child elements. A plus (+) or
minus sign (-) to the left of the elements can be clicked to expand or collapse the element
structure. To view the raw XML source (without the + and - signs), select "View Page Source" or
"View Source" from the browser menu.
Note: In Netscape, Opera, and Safari, only the element text will be displayed. To view the raw
XML, you must right click the page and select "View Source"
If an erroneous XML file is opened, the browser will report the error.
XML documents do not carry information about how to display the data.
Since XML tags are "invented" by the author of the XML document, browsers do not know if a
tag like <table> describes an HTML table or a dining table.
Without any information about how to display the data, most browsers will just display the XML
document as it is.
In the next chapters, we will take a look at different solutions to the display problem, using CSS,
XSLT and JavaScript
Look at this XML file: note.xml
With CSS (Cascading Style Sheets) you can add display information to an XML document.
Below is a fraction of the XML file. The second line links the XML file to the CSS file:
XSLT (eXtensible Stylesheet Language Transformations) is far more sophisticated than CSS.
One way to use XSLT is to transform XML into HTML before it is displayed by the browser as
demonstrated in these examples:
Below is a fraction of the XML file. The second line links the XML file to the XSLT file:
If you want to learn more about XSLT, find our XSLT tutorial on our homepage.
In the example above, the XSLT transformation is done by the browser, when the browser reads
the XML file.
Different browsers may produce different result when transforming XML with XSLT. To reduce
this problem the XSLT transformation can be done on the server.
Note that the result of the output is exactly the same, either the transformation is done by the web
server or by the web browser.
XML Parser
Most browsers have a build-in XML parser to read and manipulate XML.
Examples
W3Schools examples are browser and platform independent. These examples work in all modern
browsers.
Parsing XML
All modern browsers have a build-in XML parser that can be used to read and manipulate XML.
The parser reads XML into memory and converts it into an XML DOM object that can be
accessed with JavaScript.
There are some differences between Microsoft's XML parser and the parsers used in other
browsers. The Microsoft parser supports loading of both XML files and XML strings (text),
while other browsers use separate parsers. However, all parsers contain functions to traverse
XML trees, access, insert, and delete nodes (elements) and their attributes.
Note: When we talk about parsing XML, we often use the term "Nodes" about XML elements.
The following JavaScript fragment loads an XML document ("note.xml") into the parser:
Example explained:
• The first line of the script above creates an empty Microsoft XML
document object.
• The second line turns off asynchronized loading, to make sure that the
parser will not continue execution of the script before the document is
fully loaded.
• The third line tells the parser to load an XML document called
"note.xml".
The following JavaScript fragment loads a string called txt into the parser:
Note: The loadXML() method is used for loading strings (text), load() is used for loading files.
The following JavaScript fragment loads an XML document ("note.xml") into the parser:
var xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async="false";
xmlDoc.load("note.xml");
Example explained:
• The first line of the script above creates an empty XML document
object.
• The second line turns off synchronized loading, to make sure that the
parser will not continue execution of the script before the document is
fully loaded.
• The third line tells the parser to load an XML document called
"note.xml".
The following JavaScript fragment loads a string called txt into the parser:
Example explained:
• The first line of the script above creates an empty XML document
object.
• The second line tells the parser to load a string called txt.
Note: Internet Explorer uses the loadXML() method to parse an XML string, while other
browsers uses the DOM Parser object.
For security reasons, modern browsers do not allow access across domains.
This means, that both the web page and the XML file it tries to load, must be located on the same
server.
The examples on W3Schools all open XML files located on the W3Schools domain.
If you want to use the example above on one of your web pages, the XML files you load must be
located on your own server. Otherwise the xmlDoc.load() method, will generate the error
"Access is denied".
In the next chapter of this tutorial, you will learn how to access and retrieve data from the XML
document object (the XML DOM).
XML DOM
The DOM (Document Object Model) defines a standard way for accessing and manipulating
documents.
The DOM views XML documents as a tree-structure. All elements can be accessed through the
DOM tree. Their content (text and attributes) can be modified or deleted, and new elements can
be created. The elements, their text, and their attributes are all known as nodes.
In the examples below we use the following DOM reference to get the text from the <to>
element:
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue
The HTML DOM (HTML Document Object Model) defines a standard way for accessing and
manipulating HTML documents.
In the examples below we use the following DOM reference to change the text of the HTML
element where id="to":
document.getElementById("to").innerHTML=
The following code loads an XML document ("note.xml") into the XML parser:
<html>
<head>
<script type="text/javascript">
function parseXML()
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e)
{
alert(e.message);
return;
}
}
xmlDoc.async=false;
xmlDoc.load("note.xml");
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
}
</script>
</head>
<body onload="parseXML()">
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</p>
</body>
</html>
Output:
Important Note
To extract the text "Jani" from the XML, the syntax is:
getElementsByTagName("from")[0].childNodes[0].nodeValue
In the XML example there is only one <from> tag, but you still have to specify the array index
[0], because the XML parser method getElementsByTagName() returns an array of all <from>
nodes.
<html>
<head>
<script type="text/javascript">
function parseXML()
{
text="<note>";
text=text+"<to>Tove</to>";
text=text+"<from>Jani</from>";
text=text+"<heading>Reminder</heading>";
text=text+"<body>Don't forget me this weekend!</body>";
text=text+"</note>";
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(text);
}
catch(e)
{
try // Firefox, Mozilla, Opera, etc.
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(text,"text/xml");
}
catch(e)
{
alert(e.message);
return;
}
}
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
}
</script>
</head>
<body onload="parseXML()">
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</p>
</body>
</html>
Output:
W3Schools Internal Note
To: Tove
From: Jani
Message: Don't forget me this weekend!
<html>
<head>
<script type="text/javascript">
function parseXML()
text="<note>";
text=text+"<to>Tove</to>";
text=text+"<from>Jani</from>";
text=text+"<heading>Reminder</heading>";
text=text+"</note>";
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(text);
catch(e)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(text,"text/xml");
catch(e)
alert(e.message);
return;
document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childN
odes[0].nodeValue;
document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].c
hildNodes[0].nodeValue;
document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[
0].childNodes[0].nodeValue;
</script>
</head>
<body onload="parseXML()">
</p>
</body>
</html>
In the last chapter, we explained how to parse XML and access the DOM with JavaScript.
In this example, we loop through an XML file (cd_catalog.xml), and display each CD element as
an HTML table row:
<html>
<body>
<script type="text/javascript">
var xmlDoc=null;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load("cd_catalog.xml");
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
}
</script>
</body>
</html>
Example explained
• We check the browser, and load the XML using the correct parser
• We create an HTML table with <table border="1">
• We use getElementsByTagName() to get all XML CD nodes
• For each CD node, we display data from ARTIST and TITLE as table
data.
• We end the table with </table>
The examples on W3Schools all open XML files located on the W3Schools domain.
If you want to use the example above on one of your web pages, the XML files you load must be
located on your own server. Otherwise the xmlDoc.load() method, will generate the error
"Access is denied".
The XMLHttpRequest object provides a way to communicate with a server after a web page has
loaded.
• Update a web page with new data without reloading the page
• Request data from a server after the page has loaded
• Receive data from a server after the page has loaded
• Send data to a server in the background
Example
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for all new browsers
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5 and IE6
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}
function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = OK
// ...our code here...
}
else
{
alert("Problem retrieving XML data");
}
}
}
</script>
True means that the script continues to run after the send() method, without waiting for a
response from the server.
The onreadystatechange event complicates the code. But it is the safest way if you want to
prevent the code from stopping if you don't get a response from the server.
By setting the parameter to "false", your can avoid the extra onreadystatechange code. Use this if
it's not important to execute the rest of the code if the request fails.
However, the W3C DOM Level 3 "Load and Save" specification contains some similar
functionality, but these are not implemented in any browsers yet.
XML Application
To load the XML document (cd_catalog.xml), we use the same code as we used in the XML
Parser chapter:
var xmlDoc;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{// code for Firefox, Mozilla, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
xmlDoc.async=false;
xmlDoc.load("cd_catalog.xml");
After the execution of this code, xmlDoc is an XML DOM object, accessible by JavaScript.
The following code displays an HTML table filled with data from the XML DOM object:
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (var i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
For each CD element in the XML document, a table row is created. Each table row contains two
table data cells with ARTIST and TITLE data from the current CD element.
XML data can be copied into any HTML element that can display text.
The code below is part of the <head> section of the HTML file. It gets the XML data from the
first <CD> element and displays it in the HTML element with the id="show":
var x=xmlDoc.getElementsByTagName("CD");
i=0;
function display()
{
artist=
(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
title=
(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
year=
(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
txt="Artist: "+artist+"<br />Title: "+title+"<br />Year: "+year;
document.getElementById("show").innerHTML=txt;
}
The body of the HTML document contains an onload eventattribute that will call the display()
function when the page has loaded. It also contains a <div id='show'> element to receive the
XML data.
<body onload="display()">
<div id='show'></div>
</body>
To add navigation to the example above, create two functions called next() and previous():
function next()
{
if (i<x.length-1)
{
i++;
display();
}
}
function previous()
{
if (i>0)
{
i--;
display();
}
}
The next() function makes sure that nothing is displayed if you already are at the last CD
element, and the previous () function makes sure that nothing is displayed if you already are at
the first CD element.
The next() and previous() functions are called by clicking next/previous buttons:
XML Namespaces
Name Conflicts
In XML, element names are defined by the developer. This often results in a conflict when trying
to mix XML documents from different XML applications.
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
If these XML fragments were added together, there would be a name conflict. Both contain a
<table> element, but the elements have different content and meaning.
This XML carries information about an HTML table, and a piece of furniture:
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
In the example above, there will be no conflict because the two <table> elements have different
names.
When using prefixes in XML, a so-called namespace for the prefix must be defined.
The namespace is defined by the xmlns attribute in the start tag of an element.
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
In the example above, the xmlns attribute in the <table> tag give the h: and f: prefixes a qualified
namespace.
When a namespace is defined for an element, all child elements with the same prefix are
associated with the same namespace.
Namespaces can be declared in the elements where they are used or in the XML root element:
<root
xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
Note: The namespace URI is not used by the parser to look up information.
The purpose is to give the namespace a unique name. However, often companies use the
namespace as a pointer to a web page containing namespace information.
Try to go to http://www.w3.org/TR/html4/.
The most common URI is the Uniform Resource Locator (URL) which identifies an Internet
domain address. Another, not so common type of URI is the Universal Resource Name (URN).
Default Namespaces
Defining a default namespace for an element saves us from using prefixes in all the child
elements. It has the following syntax:
xmlns="namespaceURI"
This XML carries HTML table information:
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table xmlns="http://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
XSLT is an XML language that can be used to transform XML documents into other formats,
like HTML.
In the XSLT document below, you can see that most of the tags are HTML tags.
The tags that are not HTML tags have the prefix xsl, identified by the namespace
xmlns:xsl="http://www.w3.org/1999/XSL/Transform":
XML CDATA
When an XML element is parsed, the text between the XML tags is also parsed:
The parser does this because XML elements can contain other elements, as in this example,
where the <name> element contains two other elements (first and last):
<name><first>Bill</first><last>Gates</last></name>
<name>
<first>Bill</first>
<last>Gates</last>
</name>
Parsed Character Data (PCDATA) is a term used about text data that will be parsed by the XML
parser.
The term CDATA is used about text data that should not be parsed by the XML parser.
"&" will generate an error because the parser interprets it as the start of an character entity.
Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script
code can be defined as CDATA.
<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}
}
]]>
</script>
In the example above, everything inside the CDATA section is ignored by the parser.
A CDATA section cannot contain the string "]]>". Nested CDATA sections are not allowed.
The "]]>" that marks the end of the CDATA section cannot contain spaces or line breaks.
XML Encoding
XML documents can contain non ASCII characters, like Norwegian æ ø å , or French ê è é.
To avoid errors, specify the XML encoding, or save XML files as Unicode.
If you load an XML document, you can get two different errors indicating encoding problems:
An invalid character was found in text content.
You get this error if your XML contains non ASCII characters, and the file was saved as single-
byte ANSI (or ASCII) with no encoding specified.
You get this error if your XML file was saved as double-byte Unicode (or UTF-16) with a single-
byte encoding (Windows-1252, ISO-8859-1, UTF-8) specified.
You also get this error if your XML file was saved with single-byte ANSI (or ASCII), with
double-byte encoding (UTF-16) specified.
Windows Notepad
If you select "Save as...", you can specify double-byte Unicode (UTF-16).
Save the XML file below as Unicode (note that the document does not contain any encoding
attribute):
<?xml version="1.0"?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Norwegian: æøå. French: êèé</message>
</note>
The file above, note_encode_none_u.xml will NOT generate an error. But if you specify a single-
byte encoding it will.
Conclusion
XML files are plain text files just like HTML files.
XML files can be stored on an Internet server exactly the same way as HTML files.
Save the file on your web server with a proper name like "note.xml".
To generate an XML response from the server - simply write the following code and save it as an
ASP file on the web server:
<%
response.ContentType="text/xml"
response.Write("<?xml version='1.0' encoding='ISO-8859-1'?>")
response.Write("<note>")
response.Write("<from>Jani</from>")
response.Write("<to>Tove</to>")
response.Write("<message>Remember me this weekend</message>")
response.Write("</note>")
%>
Note that the content type of the response must be set to "text/xml".
To generate an XML response from the server using PHP, use following code:
<?php
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
echo "<note>";
echo "<from>Jani</from>";
echo "<to>Tove</to>";
echo "<message>Remember me this weekend</message>";
echo "</note>";
?>
Note that the content type of the response header must be set to "text/xml".
See how the PHP file will be returned from the server.
If you want to study PHP, you will find our PHP tutorial on our homepage.
XML can be generated from a database without any installed XML software.
To generate an XML database response from the server, simply write the following code and
save it as an ASP file on the web server:
<%
response.ContentType = "text/xml"
set conn=Server.CreateObject("ADODB.Connection")
conn.provider="Microsoft.Jet.OLEDB.4.0;"
conn.open server.mappath("/db/database.mdb")
sql="select fname,lname from tblGuestBook"
set rs=Conn.Execute(sql)
response.write("<?xml version='1.0' encoding='ISO-8859-1'?>")
response.write("<guestbook>")
while (not rs.EOF)
response.write("<guest>")
response.write("<fname>" & rs("fname") & "</fname>")
response.write("<lname>" & rs("lname") & "</lname>")
response.write("</guest>")
rs.MoveNext()
wend
rs.close()
conn.close()
response.write("</guestbook>")
%>
See the real life database output from the ASP file above.
If you want to study ASP and ADO, you will find the tutorials on our homepage.
<%
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("simple.xml"))
'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("simple.xsl"))
'Transform file
Response.Write(xml.transformNode(xsl))
%>
Example explained
• The first block of code creates an instance of the Microsoft XML parser
(XMLDOM), and loads the XML file into memory.
• The second block of code creates another instance of the parser and
loads the XSL file into memory.
• The last line of code transforms the XML document using the XSL
document, and sends the result as XHTML to your browser. Nice!
This ASP example creates a simple XML document and saves it on the server:
<%
text="<note>"
text=text & "<to>Tove</to>"
text=text & "<from>Jani</from>"
text=text & "<heading>Reminder</heading>"
text=text & "<body>Don't forget me this weekend!</body>"
text=text & "</note>"
set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.loadXML(text)
xmlDoc.Save("test.xml")
%>
The XML DOM (Document Object Model) defines a standard way for accessing and
manipulating XML documents.
The DOM views XML documents as a tree-structure. All elements can be accessed through the
DOM tree. Their content (text and attributes) can be modified or deleted, and new elements can
be created. The elements, their text, and their attributes are all known as nodes.
In an earlier chapter of this tutorial we introduced the XML DOM , and used the XML DOM
getElementsByTagName() method to retrieve data from a DOM tree.
In this chapter we will describe some other commonly used XML DOM methods. In the
examples, we have used the XML file books.xml, and a JavaScript function to load the XML file
into an DOM object called xmlDoc.
To learn all about the XML DOM, please visit our XML DOM tutorial.
Get the Value of an Element
The following code retrieves the text value of the first <title> element:
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;
The following code retrieves the text value of the "lang" attribute of the first <title> element:
txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");
The following code changes the text value of the first <title> element:
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
The setAttribute() method can be used to change the value of an existing attribute, or to create a
new attribute.
The following code adds a new attribute called "edition" (with the value "first") to each <book>
element:
x=xmlDoc.getElementsByTagName("book");
for(i=0;i<x.length;i++)
{
x[i].setAttribute("edition","first");
}
Create an Element
The createElement() method creates a new element node.
The appendChild() method adds a child node to a node (after the last child).
To create a new element with text content, it is necessary to create both an element node and a
text node.
The following code creates an element (<edition>), and adds it to the first <book> element:
newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("First");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book");
x[0].appendChild(newel);
Example explained:
Remove an Element
The following code fragment will remove the first node in the first <book> element:
x=xmlDoc.getElementsByTagName("book")[0];
x.removeChild(x.childNodes[0]);
Note: The result of the example above may be different depending on what browser you use.
Firefox treats new lines as empty text nodes, Internet Explorer don't. You can read more about
this and how to avoid it in the XML DOM tutorial.
XML Don't
Here are some technologies you should try to avoid when using XML.
Internet Explorer - XML Data Islands
What is it? An XML data island is XML data embedded into an HTML page.
Why avoid it? XML Data Islands only works with Internet Explorer browsers.
What to use instead? You should use JavaScript and XML DOM to parse and display XML in
HTML.
Bind the XML document to an <xml> tag in the HTML document. The id attribute defines an id
for the data island, and the src attribute points to the XML file:
<html>
<body>
</body>
</html>
The datasrc attribute of the <table> tag binds the HTML table to the XML data island.
The <span> tags allow the datafld attribute to refer to the XML element to be displayed. In this
case, "ARTIST" and "TITLE". As the XML is read, additional rows are created for each <CD>
element.
Why avoid it? The behavior attribute is only supported by Internet Explorer.
What to use instead? Use JavaScript and XML DOM (or HTML DOM) instead.
The following HTML file has a <style> element that defines a behavior for the <h1> element:
<html>
<head>
<style type="text/css">
h1 { behavior: url(behave.htc) }
</style>
</head>
<body>
<h1>Mouse over me!!!</h1>
</body>
</html>
<script type="text/javascript">
function hig_lite()
{
element.style.color='red';
}
function low_lite()
{
element.style.color='blue';
}
</script>
The behavior file contains a JavaScript and event handlers for the elements.
The following HTML file has a <style> element that defines a behavior for elements with an id
of "typing":
<html>
<head>
<style type="text/css">
#typing
{
behavior:url(typing.htc);
font-family:'courier new';
}
</style>
</head>
<body>
<span id="typing" speed="100">IE5 introduced DHTML behaviors.
Behaviors are a way to add DHTML functionality to HTML elements
with the ease of CSS.<br /><br />How do behaviors work?<br />
By using XML we can link behaviors to any element in a web page
and manipulate that element.</p>
</span>
</body>
</html>
<head>
<style type="text/css">
#typing
behavior:url(typing.htc);
font-family:'courier new';
</style>
</head>
<body>
<span id="typing" speed="100">IE5 introduced DHTML behaviors. Behaviors are a way to add
DHTML functionality to HTML elements with the ease of CSS.<br /><br />How does behaviors
work?<br />By using XML we can link behaviors to any element in a web page and manipulate
that element.</p>
</span>
</body>
</html>
• XSLT (XSL Transform) - transforms XML into other formats, like HTML
• XSL-FO (XSL Formatting Objects)- for formatting XML to screen, paper,
etc
• XPath - a language for navigating XML documents
Using such a standard makes it easier for both news producers and news consumers to produce,
receive, and archive any kind of news information across different hardware, software, and
programming languages.
<nitf>
<head>
<title>Colombia Earthquake</title>
</head>
<body>
<headline>
<hl1>143 Dead in Colombia Earthquake</hl1>
</headline>
<byline>
<bytag>By Jared Kotler, Associated Press Writer</bytag>
</byline>
<dateline>
<location>Bogota, Colombia</location>
<date>Monday January 25 1999 7:28 ET</date>
</dateline>
</body>
</nitf>
An example of an XML national weather service from NOAA (National Oceanic and
Atmospheric Administration):
XML DOM
The XML DOM (Document Object Model) defines a standard way for accessing and
manipulating XML documents.
The DOM presents an XML document as a tree structure, with elements, attributes, and text as
nodes:
Knowing the XML DOM is a must for anyone working with XML.
XML DOM Introduction
The XML DOM defines a standard for accessing and manipulating XML.
Before you continue you should have a basic understanding of the following:
• HTML
• XML
• JavaScript
If you want to study these subjects first, find the tutorials on our Home page.
The DOM defines a standard for accessing documents like XML and HTML:
"The W3C Document Object Model (DOM) is a platform and language-neutral interface that
allows programs and scripts to dynamically access and update the content, structure, and style
of a document."
The DOM defines the objects and properties of all document elements, and the methods
(interface) to access them.
The HTML DOM defines the objects and properties of all HTML elements, and the methods
(interface) to access them.
If you want to study the HTML DOM, find the HTML DOM tutorial on our homepage.
The XML DOM defines the objects and properties of all XML elements, and the methods
(interface) to access them.
In other words:
The XML DOM is a standard for how to get, change, add, or delete XML elements.
DOM Nodes
DOM Example
The root node in the XML above is named <bookstore>. All other nodes in the document are
contained within <bookstore>.
The first <book> node holds four nodes: <title>, <author>, <year>, and <price>, which contains
one text node each, "Everyday Italian", "Giada De Laurentiis", "2005", and "30.00".
In this example: <year>2005</year>, the element node <year>, holds a text node with the value
"2005".
"2005" is not the value of the <year> element!
The XML DOM views an XML document as a tree-structure. The tree structure is called a node-
tree.
All nodes can be accessed through the tree. Their contents can be modified or deleted, and new
elements can be created.
The node tree shows the set of nodes, and the connections between them. The tree starts at the
root node and branches out to the text
The nodes in the node tree have a hierarchical relationship to each other.
The terms parent, child, and sibling are used to describe the relationships. Parent nodes have
children. Children on the same level are called siblings (brothers or sisters).
The following image illustrates a part of the node tree and the relationship between the nodes:
Because the XML data is structured in a tree form, it can be traversed without knowing the exact
structure of the tree and without knowing the type of data contained within.
You will learn more about traversing the node tree in a later chapter of this tutorial.
In the XML above, the <title> element is the first child of the <book> element, and the <price>
element is the last child of the <book> element.
Furthermore, the <book> element is the parent node of the <title>, <author>, <year>, and
<price> elements.
Most browsers have a build-in XML parser to read and manipulate XML.
Examples
W3Schools examples are browser and platform independent. These examples work in all modern
browsers.
Parsing XML
All modern browsers have a build-in XML parser that can be used to read and manipulate XML.
The parser reads XML into memory and converts it into an XML DOM object that can be
accesses with JavaScript.
There are some differences between Microsoft's XML parser and the parsers used in other
browsers. The Microsoft parser supports loading of both XML files and XML strings (text),
while other browsers use separate parsers. However, all parsers contain functions to traverse
XML trees, access, insert, and delete nodes.
In this tutorial we will show you how to create scripts that will work in both Internet Explorer
and other browsers.
The following JavaScript fragment loads an XML document ("books.xml") into the parser:
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("books.xml");
Code explained:
The following JavaScript fragment loads a string called txt into the parser:
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);
Note: The loadXML() method is used for loading strings (text), load() is used for loading files.
The following JavaScript fragment loads an XML document ("books.xml") into the parser:
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async="false";
xmlDoc.load("books.xml");
Code explained:
• The first line creates an empty XML document object.
• The second line turns off asynchronized loading, to make sure that the
parser will not continue execution of the script before the document is
fully loaded.
• The third line tells the parser to load an XML document called
"books.xml".
The following JavaScript fragment loads a string called txt into the parser:
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
Code explained:
Note: Internet Explorer uses the loadXML() method to parse an XML string, while other
browsers uses the DOMParser object.
The following example loads an XML document ("books.xml") into the XML parser:
<html>
<body>
<script type="text/javascript">
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e) {alert(e.message)}
}
try
{
xmlDoc.async=false;
xmlDoc.load("books.xml");
document.write("xmlDoc is loaded, ready for use");
}
catch(e) {alert(e.message)}
</script>
</body>
</html>
Try it yourself
For security reasons, modern browsers does not allow access across domains.
This means, that both the web page and the XML file it tries to load, must be located on the same
server.
The examples on W3Schools all open XML files located on the W3Schools domain.
If you want to use the example above on one of your web pages, the XML files you load must be
located on your own server. Otherwise the xmlDoc.load() method, will generate the error
"Access is denied".
<html>
<body>
<script type="text/javascript">
text="<bookstore>"
text=text+"<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</author>";
text=text+"<year>2005</year>";
text=text+"</book>";
text=text+"</bookstore>";
The XML DOM contains methods (functions) to traverse XML trees, access, insert, and delete
nodes.
However, before an XML document can be accessed and manipulated, it must be loaded into an
XML DOM object.
The previous chapter demonstrated how to load XML documents. To make it simpler to maintain
this code, it should be written as a function:
function loadXMLDoc(dname)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e) {alert(e.message)}
}
try
{
xmlDoc.async=false;
xmlDoc.load(dname);
return(xmlDoc);
}
catch(e) {alert(e.message)}
return(null);
}
The function above can be stored in the <head> section of an HTML page, and called from a
script in the page.
Try it yourself
To make the function above even easier to maintain, and to make sure the same code is used in
all pages, it can be stored in an external file.
The file can be loaded in the <head> section of an HTML page, and loadXMLDoc() can be
called from a script in the page:
<html>
<head>
<script type="text/javascript" src="loadxmldoc.js">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
document.write("xmlDoc is loaded, ready for use");
</script>
</body>
</html>
Try it yourself
A similar function can be used to load an XML string (instead an XML file):
function loadXMLString(txt)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);
return(xmlDoc);
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
return(xmlDoc);
}
catch(e) {alert(e.message)}
}
return(null);
}
To make the function above easier to maintain, and to make sure the same code is used in all
pages, it can be stored in an external file.
Properties and methods define the programming interface to the XML DOM.
Examples
Programming Interface
The DOM models XML as a set of node objects. The nodes can be accessed with JavaScript or
other programming languages. In this tutorial we use JavaScript.
The programming interface to the DOM is defined by a set standard properties and methods.
Methods are often referred to as something that is done (i.e. delete "book").
Example
The JavaScript code to get the text from the first <title> element in books.xml:
txt=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue
After the execution of the statement, txt will hold the value "Everyday Italian"
Explained:
The following code fragment uses the loadXMLDoc function (described in the previous chapter)
to load books.xml into the XML parser, and displays data from the first book:
xmlDoc=loadXMLDoc("books.xml");
document.write(xmlDoc.getElementsByTagName("title")
[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("author")
[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("year")
[0].childNodes[0].nodeValue);
Output:
Everyday Italian
Giada De Laurentiis
2005
Try it yourself
In the example above we use childNodes[0] for each text node, even if there is only one text
node for each element. This is because the getElementsByTagName() method always returns an
array.
The following code fragment uses the loadXMLString function (described in the previous
chapter) to load books.xml into the XML parser, and displays data from the first book:
text="<bookstore>"
text=text+"<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</author>";
text=text+"<year>2005</year>";
text=text+"</book>";
text=text+"</bookstore>";
xmlDoc=loadXMLString(text);
document.write(xmlDoc.getElementsByTagName("title")
[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("author")
[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("year")
[0].childNodes[0].nodeValue);
Output:
Everyday Italian
Giada De Laurentiis
2005
With the DOM, you can access every node in an XML document.
Examples
<html>
<head>
<script type="text/javascript" src="loadxmldoc.js"></script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
document.write(x[2].childNodes[0].nodeValue);
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
for (i=0;i<x.length;i++)
document.write(x[i].childNodes[0].nodeValue);
document.write("<br />");
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
document.write(xmlDoc.documentElement.nodeName);
document.write("<br />");
document.write(xmlDoc.documentElement.nodeType);
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
if (x[i].nodeType==1)
document.write(x[i].nodeName);
document.write("<br />");
}
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
y=xmlDoc.getElementsByTagName("book")[0].firstChild;
for (i=0;i<x.length;i++)
if (y.nodeType==1)
}
y=y.nextSibling;
</script>
</body>
</html>
Accessing Nodes
Syntax
node.getElementsByTagName("tagname");
Example
The following example returns all <title> elements under the x element:
x.getElementsByTagName("title");
Note that the example above only returns <title> elements under the x node. To return all <title>
elements in the XML document use:
xmlDoc.getElementsByTagName("title");
The getElementsByTagName() method returns a node list. A node list is an array of nodes.
The following code loads "books.xml" into xmlDoc using loadXMLDoc() and stores a list of
<title> nodes (a node list) in the variable x:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
The <title> elements in x can be accessed by index number. To access the third <title> you can
write:
y=x[2];
Try it yourself.
You will learn more about node lists in a later chapter of this tutorial.
The length property defines the length of a node list (the number of nodes).
You can loop through a node list by using the length property:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write("<br />");
}
Example explained:
Try it yourself.
Node Types
You will learn more about the node properties in the next chapter of this tutorial.
Try it yourself.
Traversing Nodes
The following code loops through the child nodes, that are also element nodes, of the root node:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
{
if (x[i].nodeType==1)
{//Process only element nodes (type 1)
document.write(x[i].nodeName);
document.write("<br />");
}
}
Example explained:
Try it yourself.
The following code navigates the node tree using the node relationships:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
y=xmlDoc.getElementsByTagName("book")[0].firstChild;
for (i=0;i<x.length;i++)
{
if (y.nodeType==1)
{//Process only element nodes (type 1)
document.write(y.nodeName + "<br />");
}
y=y.nextSibling;
}
Examples
<html>
<head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
document.write(xmlDoc.documentElement.nodeName);
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;
document.write(txt);
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;
document.write(txt);
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;
document.write(txt);
</script>
</body>
</html>
Node Properties
Objects have methods (functions) and properties (information about the object), that can be
accessed and manipulated by JavaScript.
• nodeName
• nodeValue
• nodeType
• nodeName is read-only
• nodeName of an element node is the same as the tag name
• nodeName of an attribute node is the attribute name
• nodeName of a text node is always #text
• nodeName of the document node is always #document
Try it yourself.
The following code retrieves the text node value of the first <title> element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;
Example explained:
Try it yourself
The following code changes the text node value of the first <title> element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
Example explained:
Try it yourself
Element 1
Attribute 2
Text 3
Comment 8
Document 9
A list of nodes is returned by the getElementsByTagName() method and the childNodes property.
Examples
<html>
<head>
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
txt=x[0].childNodes[0].nodeValue;
document.write(txt);
</script>
</body>
</html>
<html>
<head>
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('title');
for (i=0;i<x.length;i++)
document.write(x[i].childNodes[0].nodeValue);
document.write("<br />");
</script>
</body>
</html>
<html>
<head>
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].attributes;
document.write(x.getNamedItem("category").nodeValue);
</script>
</body>
</html>
DOM Node List
A node list object represents a list of nodes, in the same order as in the XML.
Nodes in the node list are accessed with index numbers starting from 0.
The following image represents a node list of the <title> elements in "books.xml":
The following code fragment loads "books.xml" into xmlDoc using loadXMLDoc() and returns a
node list of title elements in "books.xml":
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
The following code fragment returns the text from the first <title> element in the node list (x):
txt=x[0].childNodes[0].nodeValue;
Try it yourself.
A node list object keeps itself up-to-date. If an element is deleted or added, the list is
automatically updated.
The length property of a node list is the number of nodes in the list.
The following code fragment loads "books.xml" into xmlDoc using loadXMLDoc() and returns
the number of <title> elements in "books.xml":
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('title').length;
The length of the node list can be used to loop through all the elements in the list.
The following code fragment uses the length property to loop through the list of <title> elements:
xmlDoc=loadXMLDoc("books.xml");
//the x variable will hold a node list
x=xmlDoc.getElementsByTagName('title');
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write("<br />");
}
Output:
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Example explained:
Try it yourself.
This is called a named node map, and is similar to a node list, except for some differences in
methods and properties.
A attribute list keeps itself up-to-date. If an attribute is deleted or added, the list is automatically
updated.
The following code fragment loads "books.xml" into xmlDoc using loadXMLDoc() and returns a
list of attribute nodes from the first <book> element in "books.xml":
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book')[0].attributes;
After the execution of the code above, x.length = is the number of attributes and
x.getNamedItem() can be used to return an attribute node.
The following code fragment displays the value of the "category" attribute, and the number of
attributes, of a book:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].attributes;
document.write(x.getNamedItem("category").nodeValue);
document.write("<br />" + x.length);
Output:
cooking
1
Example explained:
Examples
<html>
<head>
</head>
<body>
<script type="text/javascript">
text="<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</author>";
text=text+"<year>2005</year>";
text=text+"</book>";
xmlDoc=loadXMLString(text);
// documentElement always represents the root node
x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
document.write(x[i].nodeName);
document.write(": ");
document.write(x[i].childNodes[0].nodeValue);
document.write("<br />");
</script>
</body>
</html>
Often you want to loop an XML document, for example: when you want to extract the value of
each element.
The example below loops through all child nodes of <book>, and displays their names and
values:
<html>
<head>
<script type="text/javascript" src="loadxmlstring.js"></script>
</head>
<body>
<script type="text/javascript">
text="<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</author>";
text=text+"<year>2005</year>";
text=text+"</book>";
xmlDoc=loadXMLString(text);
Output:
Example explained:
Different browsers handle empty text nodes in the XML DOM differently.
Examples
<head>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement.childNodes;
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
if (x[i].nodeType==1)
document.write(x[i].nodeName);
document.write("<br />");
</script>
</body>
</html>
However, there are some differences between browsers. Two important differences are:
The different ways to load XML is explained in the chapter "DOM Parser".
The different ways to handle white spaces and new lines is explained in this chapter.
The following example (edited by Notepad) contains CR/LF (new line) between each line and
two spaces in front of each child node:
<book>
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
Firefox, and some other browsers, will treat empty white-spaces or new lines as text nodes,
Internet Explorer will not
The following code fragment displays how many child nodes the root element (of books.xml)
has:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement.childNodes;
document.write("Number of child nodes: " + x.length);
Example explained:
Try it yourself
To ignore empty text nodes between element nodes, you can check the node type. An element
node has type 1:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
{
if (x[i].nodeType==1)
{// only process element nodes
document.write(x[i].nodeName);
document.write("<br />");
}
}
Example explained:
Examples
<html>
<head>
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
document.write(x.parentNode.nodeName);
</script>
</body>
</html>
<html>
<head>
</script>
<script type="text/javascript">
function get_firstChild(n)
y=n.firstChild;
while (y.nodeType!=1)
y=y.nextSibling;
}
return y;
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=get_firstChild(xmlDoc.getElementsByTagName("book")[0]);
document.write(x.nodeName);
</script>
</body>
</html>
Accessing nodes in the node tree via the relationship between nodes, is often called "navigating
nodes".
In the XML DOM, node relationships are defined as properties to the nodes:
• parentNode
• childNodes
• firstChild
• lastChild
• nextSibling
• previousSibling
The following image illustrates a part of the node tree and the relationship between nodes in
books.xml:
All nodes has exactly one parent node. The following code navigates to the parent node of
<book>:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
document.write(x.parentNode.nodeName);
Example explained:
Try it yourself
This causes a problem when using the properties: firstChild, lastChild, nextSibling,
previousSibling.
To avoid navigating to empty text nodes (spaces and new-line characters between element
nodes), we use a function that checks the node type:
function get_nextSibling(n)
{
y=n.nextSibling;
while (y.nodeType!=1)
{
y=y.nextSibling;
}
return y;
}
The function above allows you to use get_nextSibling(node) instead of the property
node.nextSibling.
Code explained:
Element nodes are type 1. If the sibling node is not an element node, it moves to the next nodes
until an element node is found. This way, the result will be the same in both Internet Explorer
and Firefox.
The following code displays the first element node of the first <book>:
<html>
<head>
<script type="text/javascript" src="loadxmldoc.js">
</script>
<script type="text/javascript">
//check if the first node is an element node
function get_firstChild(n)
{
y=n.firstChild;
while (y.nodeType!=1)
{
y=y.nextSibling;
}
return y;
}
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
x=get_firstChild(xmlDoc.getElementsByTagName("book")[0]);
document.write(x.nodeName);
</script>
</body>
</html>
Output:
title
Example explained:
Examples
In the DOM, everything is a node. Element nodes does not have a text value.
The text of an element node is stored in a child node. This node is called a text node.
The way to get the text of an element, is to get the value of the child node (text node).
The getElementsByTagName() method returns a node list containing all elements with the
specified tag name in the same order as they appear in the source document.
The following code loads "books.xml" into xmlDoc using loadXMLDoc() and retrieves the first
<title> element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
The childNodes property returns a list of child nodes. The <title> element has only one child
node. It is a text node.
The following code retrieves the text node of the <title> element:
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
The nodeValue property returns the text value of the text node:
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
txt=y.nodeValue;
Try it yourself
In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values.
The way to get the value of an attribute, is to get its text value.
This can be done using the getAttribute() method or using the nodeValue property of the attribute
node.
The following code retrieves the text value of the "lang" attribute of the first <title> element:
xmlDoc=loadXMLDoc("books.xml");
txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");
Example explained:
Try it yourself
Loop through all <book> elements and get their "category" attributes: Try it yourself
The following code retrieves the text value of the "lang" attribute of the first <title> element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang"
);
txt=x.nodeValue;
Examples
In the DOM, everything is a node. Element nodes does not have a text value.
The text of an element node is stored in a child node. This node is called a text node.
The way to change the text of an element, is to change the value of the child node (text node).
The following code changes the text node value of the first <title> element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
Example explained:
Try it yourself
Loop through and change the text node of all <title> elements: Try it yourself
In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values.
The way to change the value of an attribute, is to change its text value.
This can be done using the setAttribute() method or using the nodeValue property of the attribute
node.
The setAttribute() method changes the value of an existing attribute, or creates a new attribute.
The following code changes the category attribute of the <book> element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("category","food");
Example explained:
Loop through all <title> elements and add a new attribute: Try it yourself
Note: If the attribute does not exist, a new attribute is created (with the name and value
specified).
The nodeValue property can be used to change the value of a attribute node:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0]
y=x.getAttributeNode("category");
y.nodeValue="food";
Example explained:
Examples
When a node is removed, all its child nodes are also removed.
The following code fragment will remove the first <book> element from the loaded xml:
xmlDoc=loadXMLDoc("books.xml");
y=xmlDoc.getElementsByTagName("book")[0];
xmlDoc.documentElement.removeChild(y);
Example explained:
Try it yourself
When you have navigated to the node you want to remove, it is possible to remove that node
using the parentNode property and the removeChild() method:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
x.parentNode.removeChild(x);
Example explained:
Try it yourself
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
x.removeChild(y);
Example explained:
Try it yourself
It is not very common to use removeChild() just to remove the text from a node. The nodeValue
property can be used instead. See next paragraph.
The nodeValue property can be used to change or clear the value of a text node:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="";
Example explained:
1. Load "books.xml" into xmlDoc using loadXMLDoc()
2. Set the variable x to be the text node of the first title element
3. Use the nodeValue property to clear the text from the text node
Try it yourself
Loop through and change the text node of all <title> elements: Try it yourself
Example: removeAttribute('category')
The following code fragment removes the "category" attribute in the first <book> element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");
Example explained:
Try it yourself
Loop through and remove the "category" attribute of all <book> elements: Try it yourself
The removeAttributeNode(node) method is used to remove an attribute node, using the node
object as parameter.
Example: removeAttributeNode(x)
The following code fragment removes all the attributes of all <book> elements:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
for (i=0;i<x.length;i++)
{
while (x[i].attributes.length>0)
{
attnode=x[i].attributes[0];
old_att=x[i].removeAttributeNode(attnode);
}
}
Example explained:
Examples
x=xmlDoc.documentElement;
y=xmlDoc.getElementsByTagName("book")[0]
//replace the first book node with the new node
x.replaceChild(newNode,y);
Example explained:
Try it yourself
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.replaceData(0,8,"Easy");
Example explained:
Try it yourself
It is easier to replace the data in a text node using the nodeValue property.
The following code fragment will replace the text node value in the first <title> element with
"Easy Italian":
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Italian";
Example explained:
Examples
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
Example explained:
Try it yourself
Loop through and add an element to all <book> elements: Try it yourself
xmlDoc=loadXMLDoc("books.xml");
newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";
x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);
Example explained:
Try it yourself
Loop through all <title> elements and add a new attribute node: Try it yourself
Since the setAttribute() method creates a new attribute if the attribute does not exist, it can be
used to create a new attribute.
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");
Example explained:
Try it yourself
Loop through all <title> elements and add a new attribute: Try it yourself
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
Example explained:
Try it yourself
Add an element node, with a text node, to all <book> elements: Try it yourself
xmlDoc=loadXMLDoc("books.xml");
newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);
Example explained:
Try it yourself
Loop through, and add a CDATA section, to all <book> elements: Try it yourself
xmlDoc=loadXMLDoc("books.xml");
newComment=xmlDoc.createComment("Revised March 2008");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newComment);
Example explained:
Examples
The new node is added (appended) after any existing child nodes.
The following code fragment creates an element (<edition>), and adds it after the last child of the
first <book> element:
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
Example explained:
Try it yourself
Loop through and append an element to all <book> elements: Try it yourself
The insertBefore() method is used to insert a node before a specified child node.
This method is useful when the position of the added node is important:
xmlDoc=loadXMLDoc("books.xml");
newNode=xmlDoc.createElement("book");
x=xmlDoc.documentElement;
y=xmlDoc.getElementsByTagName("book")[3];
x.insertBefore(newNode,y);
Example explained:
Try it yourself
If the second parameter of insertBefore() is null, the new node will be added after the last
existing child node.
The setAttribute() method creates a new attribute if the attribute does not exist:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");
Example explained:
Try it yourself
Note: If the attribute already exists, the setAttribute() method will overwrite the existing value.
The following code fragment will add "Easy" to the text node of the first <title> element of the
loaded XML:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.insertData(0,"Easy ");
Examples
The cloneNode() method has a parameter (true or false). This parameter indicates if the cloned
node should include all attributes and child nodes of the original node.
The following code fragment copies the first <book> node and appends it to the root node of the
document:
xmlDoc=loadXMLDoc("books.xml");
oldNode=xmlDoc.getElementsByTagName('book')[0];
newNode=oldNode.cloneNode(true);
xmlDoc.documentElement.appendChild(newNode);
Output:
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Everyday Italian
Example explained:
• Update a web page with new data without reloading the page
• Request data from a server after the page has loaded
• Receive data from a server after the page has loaded
• Send data to a server in the background
xmlhttp=new XMLHttpRequest()
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
Example
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for all new browsers
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5 and IE6
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}
function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = OK
// ...our code here...
}
else
{
alert("Problem retrieving XML data");
}
}
}
</script>
True means that the script continues to run after the send() method, without waiting for a
response from the server.
The onreadystatechange event complicates the code. But it is the safest way if you want to
prevent the code from stopping if you don't get a response from the server.
By setting the parameter to "false", your can avoid the extra onreadystatechange code. Use this if
it's not important to execute the rest of the code if the request fails.
More Examples
XML / ASP
You can also open and send an XML document to an ASP page on the server, analyze the
request, and send back the result.
<html>
<body>
<script type="text/javascript">
xmlHttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xmlHttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlHttp!=null)
{
xmlHttp.open("GET", "note.xml", false);
xmlHttp.send(null);
xmlDoc=xmlHttp.responseText;
<%
set xmldoc = Server.CreateObject("Microsoft.XMLDOM")
xmldoc.async=false
xmldoc.load(request)
You send the result back to the client using the response.write property.
Try it yourself
However, the W3C DOM Level 3 "Load and Save" specification contains some similar
functionality, but these are not implemented in any browsers yet.
Examples
Node Types
The following table lists the different W3C node types, and which node types they may have as
children:
The following table lists what the nodeName and the nodeValue properties will return for each
node type:
1 ELEMENT_NODE
2 ATTRIBUTE_NODE
3 TEXT_NODE
4 CDATA_SECTION_NODE
5 ENTITY_REFERENCE_NODE
6 ENTITY_NODE
7 PROCESSING_INSTRUCTION_NODE
8 COMMENT_NODE
9 DOCUMENT_NODE
10 DOCUMENT_TYPE_NODE
11 DOCUMENT_FRAGMENT_NODE
12 NOTATION_NODE
The Node object is the primary data type for the entire DOM.
A node can be an element node, an attribute node, a text node, or any other of the node types
explained in the "Node types" chapter.
Notice that while all objects inherits the Node properties / methods for dealing with parents and
children, not all objects can have parents or children. For example, Text nodes may not have
children, and adding children to such nodes results in a DOM error.
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
The nodes in the NodeList can be accessed through their index number (starting from 0).
The NodeList keeps itself up-to-date. If an element is deleted or added, in the node list or the
XML document, the list is automatically updated.
Note: In a node list, the nodes are returned in the order in which they are specified in the XML.
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
The NamedNodeMap keeps itself up-to-date. If an element is deleted or added, in the node list or
the XML document, the list is automatically updated.
Note: In a named node map, the nodes are not returned in any particular order.
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
The Document object is the root of a document tree, and gives us the primary access to the
document's data.
Since element nodes, text nodes, comments, processing instructions, etc. cannot exist outside the
document, the Document object also contains methods to create these objects. The Node objects
have a ownerDocument property which associates them with the Document where they were
created.
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
normalizeDocument() NoYes
The DOMImplementation object performs operations that are independent of any particular
instance of the document object model.
The DOMImplementation object performs operations that are independent of any particular
instance of the document object model.
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
The DocumentType object provides an interface to the entities defined for an XML document.
The DocumentType object
Each document has a DOCTYPE attribute that whose value is either null or a DocumentType
object.
The DocumentType object provides an interface to the entities defined for an XML document.
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
A processing instruction is used as a way to keep processor-specific information in the text of the
XML document.
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
The Element object represents an element in an XML document. Elements may contain
attributes, other elements, or text. If an element contains text, the text is represented in a text-
node.
IMPORTANT! Text is always stored in text nodes. A common error in DOM processing is to
navigate to an element node and expect it to contain the text. However, even the simplest element
node has a text node under it. For example, in <year>2005</year>, there is an element node
(year), and a text node under it, which contains the text (2005).
Because the Element object is also a Node, it inherits the Node object's properties and methods.
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
The Attr object represents an attribute of an Element object. The allowable values for attributes
are usually defined in a DTD.
Because the Attr object is also a Node, it inherits the Node object's properties and methods.
However, an attribute does not have a parent node and is not considered to be a child node of an
element, and will return null for many of the Node properties.
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
Examples
A CDATA section contains text that will NOT be parsed by a parser. Tags inside a CDATA
section will NOT be treated as markup and entities will not be expanded. The primary purpose is
for including material such as XML fragments, without needing to escape all the delimiters.
The only delimiter that is recognized in a CDATA section is "]]>" - which indicates the end of
the CDATA section. CDATA sections cannot be nested.
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
Examples
IE: Internet Explorer, F: Firefox, O: Opera, W3C: World Wide Web Consortium (Internet
Standard)
Examples
Methods
Method Description
Properties
Property Description
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete
responseText Returns the response as a string
Microsoft's parseError object can be used to retrieve error information from the Microsoft XML
parser.
To see how Firefox handles parser errors, check out the next page of this tutorial.
With the parseError object, you can retrieve the error code, the error text, the line that caused the
error, and more.
Note: The parseError object is not a part of the W3C DOM standard!
File Error
In the following code we will try to load a non-existing file, and display some of its error
properties:
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("ksdjf.xml");
Try it yourself
XML Error
In the following code we let the parser load an XML document that is not well-formed.
(You can read more about well-formed and valid XML in our XML tutorial)
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("note_error.xml");
Property Description
srcText Returns a string containing the line that caused the error
When Firefox encounter a parser error, it loads an XML document containing the error
Unlike Internet Explorer, if Firefox encounters an error, it loads an XML document containing
the error description.
The root node name of the XML error document is "parsererror". This is used to check if there is
an error.
XML Error
In the following code we let the parser load an XML document that is not well-formed.
(You can read more about well-formed and valid XML in our XML tutorial)
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async=false;
xmlDoc.load("note_error.xml");
if (xmlDoc.documentElement.nodeName=="parsererror")
{
errStr=xmlDoc.documentElement.childNodes[0].nodeValue;
errStr=errStr.replace(/</g, "<");
document.write(errStr);
}
else
{
document.write("XML is valid");
}
Example explained:
Note: Only Internet Explorer will actually check your XML against the DTD. Firefox will not.
Here we have created an XML load function that checks for parser errors in both Internet
Explorer and Firefox:
function loadXMLDocErr(dname)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(dname);
if (xmlDoc.parseError.errorCode != 0)
{
alert("Error in line " + xmlDoc.parseError.line +
" position " + xmlDoc.parseError.linePos +
"\nError Code: " + xmlDoc.parseError.errorCode +
"\nError Reason: " + xmlDoc.parseError.reason +
"Error Line: " + xmlDoc.parseError.srcText);
return(null);
}
}
catch(e)
{
try //Firefox
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async=false;
xmlDoc.load(dname);
if (xmlDoc.documentElement.nodeName=="parsererror")
{
alert(xmlDoc.documentElement.childNodes[0].nodeValue);
return(null);
}
}
catch(e) {alert(e.message)}
}
try
{
return(xmlDoc);
}
catch(e) {alert(e.message)}
return(null);
}
The XML DOM defines a standard for accessing and manipulating XML.
The XML DOM views an XML document as a tree-structure. The tree structure is called a node-
tree.
In a node tree, the terms parent, child, and sibling are used to describe the relationships.
All modern browsers have a build-in XML parser that can be used to read and manipulate XML.
With the XML DOM properties and methods, you can access every node in an XML document.
Different browsers treat new line, or space characters, between nodes differently.
To ignore empty text nodes between element nodes, you can check the node type.
Our XML DOM examples also represent a summary of this XML DOM tutorial.
If you want to learn more about validating XML, we recommend DTD and XML Schema.
With XSLT you can transform XML documents into other formats, like XHTML.
If you want to learn more about XSLT, please visit our XSLT tutorial.
The purpose of a DTD is to define what elements, attributes and entities is legal in an XML
document.
With DTD, each of your XML files can carry a description of its own format with it.
DTD can be used to verify that the data you receive, and your own data, is valid.
If you want to learn more about DTD, please visit our DTD tutorial
Examples explained
Examples explained
XML DOM Accessing Nodes
Examples explained
Examples explained
Examples explained
Display the length of a node list - Different results in IE and other browsers
Ignore empty text between nodes
Examples explained
Examples explained
Examples explained
Examples explained
Examples explained
Examples explained
Examples explained
XML DOM Add Nodes
Examples explained
Examples explained