W3Schools
home HOME Tutorials | References | Examples | Forum | About
Search W3Schools :

XML DOM Tutorial
DOM HOME
DOM Introduction
DOM Nodes
DOM Node Tree
DOM Parsing
DOM Load Function
DOM Methods
DOM Accessing
DOM Node Info
DOM Node List
DOM Traversing
DOM Browsers
DOM Navigating

Manipulate Nodes
DOM Get Values
DOM Change Nodes
DOM Remove Nodes
DOM Replace Nodes
DOM Create Nodes
DOM Add Nodes
DOM Clone Nodes
DOM HttpRequest

XML DOM Reference
DOM Node Types
DOM Node
DOM NodeList
DOM NamedNodeMap
DOM Document
DOM DocumentImpl
DOM DocumentType
DOM ProcessingInstr
DOM Element
DOM Attribute
DOM Text
DOM CDATA
DOM Comment
DOM HttpRequest
DOM ParseError Obj
DOM Parser Errors

DOM Summary

Examples
DOM Examples
DOM Validator

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Tutorials
W3Schools Forum

Helping W3Schools

Accessing the XML DOM

prev next

One of the great things about XML is the possibility to separate presentation (HTML) from the data.


Providing HTML Content From XML Files

One of the great things about XML is the possibility to separate presentation (HTML) from the data.

By using the XML parser, an HTML page can be constructed as a static document, and an embedded JavaScript can be used to provide dynamic data.

The following example reads data from an XML document and writes the XML data into (waiting) HTML elements:

Cross Browser Example

<html>
<head>
<script type="text/javascript">
var xmlDoc;
function loadXML()
{
//load xml file
// code for IE
if (window.ActiveXObject)
  {
  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load("note.xml");
  getmessage();
  }
// code for Mozilla, etc.
else if (document.implementation &&
document.implementation.createDocument)
  {
  xmlDoc= document.implementation.createDocument("","",null);
  xmlDoc.load("note.xml");
  xmlDoc.onload=getmessage;
  }
else
  {
  alert('Your browser cannot handle this script');
  }
}
function getmessage()
{
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].firstChild.nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].firstChild.nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue;
}
</script>
</head>
<body onload="loadXML()" bgcolor="yellow">
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span>
<hr />
<b>Message:</b> <span id="message"></span>
</p>
</body>
</html>

Try it yourself

Try it yourself with VBScript

Important: To extract the text (Jani) from an element like this: <from>Jani</from>, the syntax is: getElementsByTagName("from")[0].firstChild.nodeValue, and NOT like this: getElementsByTagName("from").nodeValue. The reason for this is that the result returned from getElementsByTagName is an array of nodes, containing all the nodes within the XML document with the specified tag name (in this case "from").


Traversing the Node-tree

With VBScript, it is very simple to extract elements (and their value) from an XML document by traversing the node-tree:

<script type="text/vbscript">
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
for each x in xmlDoc.documentElement.childNodes
  document.write("<b>" & x.nodename & ":</b> ")
  document.write(x.text)
  document.write("<br />")
next
</script>

Try it yourself and also try to traverse our CD catalog example.


prev next


Learn XML with <oXygen/> XML Editor - Free Trial!

oXygen - Probably The World's Best XML Editor   

oXygen helps you learn to define, edit, validate and transform XML documents. Supported technologies include XML Schema, DTD, Relax NG, XSLT, XPath, XQuery, CSS.

Understand in no time how XSLT and XQuery work by using the intuitive oXygen debugger!

Do you have any XML related questions? Get free answers from the oXygen XML forum and from the video demonstrations.

Download a FREE 30-day trial today!



Jump to: Top of Page or HOME or Printer Friendly Printer friendly page

W3Schools provides material for training only. We do not warrant the correctness of its contents. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2008 by Refsnes Data. All Rights Reserved.

Validate Validate W3C-WAI level A conformance icon W3Schools was converted to XHTML in December 1999
E Components
Ecommerce
Components
for ASP.NET
WEB HOSTING
$15 Domain Name
Registration
Save $20 / year!
Buy UK Domain Names
Register Domain Names
Cheap Domain Names
Cheap Web Hosting
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
Web Hosting Company
UK Reseller Hosting
Web Hosting
WEB BUILDING
Website Templates
Flash Templates
Website Builder
Internet Business Opportunity
Custom Programming
FREE Trial or Demo
Web Content Manager
Forms,Web Alerts,RSS
Download XML editor
HTML Tutorial DVD
EDUCATION
US Web Design Schools
HTML Exam
TRAVEL
FUNC Cheap Flights
YELLOW PAGES
www.nettkatalogen.no
www.gulex.dk
www.gulex.se
www.gelbex.de
www.teloos.fi
www.teloos.fr
www.teloos.co.uk
www.teloos.at
www.teloos.de