w3schools
Search W3Schools :  
  
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...   References Examples Forum About
ADVERTISEMENTS

PHP MySQL Hosting
Get a Freelancer
Download XML editor

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

XML DOM Examples

DOM Examples
DOM Validator

 

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



 
WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting
WEB BUILDING
Website Templates
Flash Templates
Website Builder
Internet Business Opportunity
Get a Freelancer
Download XML editor
FREE Flash Website
FREE Web Templates
FLIGHT TICKETS
Find the cheapest flight
to any destination now!
EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
STATISTICS
Browser Statistics
Browser OS
Browser Display
W3Schools.com HOME | TOP | PRINT | FORUM | ABOUT
W3Schools is for training only. We do not warrant the correctness of its content. 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-2009 by Refsnes Data. All Rights Reserved.