E-learning Center - Only free tutorials and information

Content:

PHP book

Sybase

Free scripts

Tutorials

ASP

Free Pictures

Free Video

Free Music

Downloads

Games


Ads

Translation Service

Tutorials

Traversing the node tree
One common way to extract XML elements from an XML document is to traverse the node tree and extract the text value of each element. A small snippet of programming code like a VBScript for/each construct can be written to demonstrate this.

The following VBScript code traverses an XML node tree, and displays the XML elements in the browser:

set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")

for each x in xmlDoc.documentElement.childNodes
document.write(x.nodename)
document.write(": ")
document.write(x.text)
next

JUST TRY IT and also try to traverse our CD catalog example.


--------------------------------------------------------------------------------

Providing HTML content from XML files
One of the great promises of XML is the possibility to separate HTML documents from their data. By using an XML parser inside the browser, an HTML page can be constructed as a static document, with an embedded JavaScript to provide dynamic data.

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

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")

nodes = xmlDoc.documentElement.childNodes

to.innerText = nodes.item(0).text
from.innerText = nodes.item(1).text
header.innerText = nodes.item(2).text
body.innerText = nodes.item(3).text

JUST TRY IT


--------------------------------------------------------------------------------

Accessing XML elements by name
Addressing elements by number is not the preferred way to extract XML elements from an XML document. Using names is a better way.

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

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")

to.innerText=
xmlDoc.getElementsByTagName("to").item(0).text
from.innerText=
xmlDoc.getElementsByTagName("from").item(0).text
header.innerText=
xmlDoc.getElementsByTagName("heading").item(0).text
body.innerText=
xmlDoc.getElementsByTagName("body").item(0).text

JUST TRY IT

Important: To extract the text (Jani) from an element like this: <from>Jani</from>, the syntax is: getElementsByTagName("from").item(0).text, and NOT like this: getElementsByTagName("from").text.

The reason why you must address the child node like getElementsByTagName("from").item(0).text is that the result returned from getElementsByTagName is an array of nodes, containing all nodes found within the XML document with the specified tag name (in this case "from").


--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

Product Spotlight
Visual Basic .NET
Develop Windows, Web, and mobile apps in record time with your FREE Visual Basic .NET Resource Kit..


--------------------------------------------------------------------------------

Want To Be A Web Master?
If you want to be a Web Master, you will have to host your web site with an ISP (Internet Service Provider).

Lunarpages offers affordable, reliable and scalable hosting plans with a 30-day, money-back guarantee!

Lunarpages offers industry leading infrastructure built with the best equipment, including: Dell, Sun and Cisco Network Devices. Their datacenter, located in Los Angeles, California, provides the highest level of security, safety, redundancy, reliability, scalability and technology. They are multi backboned with Mzima Networks, NTT/Verio, Global Crossing, Genuity and Cable & Wireless.

  © Copyright 2003-04. E-Learning Center.   free web templates from myfreetemplates.com