Share on Google+Share on Google+

JavaScript XML Parser

This page discusses - JavaScript XML Parser

JavaScript XML Parser


In this section, you will learn how to access and manipulate XML document using DOM parser in javascript.

XML parser reads and loads the xml into computer memory and converts it into DOM object. After loading the document, the data can be manipulated using DOM  with JavaScript. Now the text and their values can be updated, deleted. New elements can also be created using DOM.

In the given example we used the DOM reference to get the values from the elements of XML document. The ActiveXObject("Microsoft.XMLDOM") creates the Microsoft XML document object for Internet Explorer and document.implementation.createDocument("","",null) creates XML document object for other browsers. xmlDoc.async=false indicates that the parser continues the script execution after loading of the document fully. The method xmlDoc.load("data.xml") allows the parser to load an XML document 'data.xml'. The childNodes[0].nodeValue gets the value of the node from the element.

Here is the data.xml:


Here is the code:

<h2>XML Parser</h2>
<script type="text/javascript">
function parseXML(){
try {
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
try {
<body onload="parseXML()">
<h3>Employee's Information</h3>
<b>Name:</b> <span id="n1"></span><br>
<b>Address:</b> <span id="n2"></span><br>
<b>Designition:</b> <span id="n3"></span>

Output will be displayed as:

Download Source Code:




Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: JavaScript XML Parser  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Can ┼?ahin Bak─▒r
October 26, 2011

thanks for sharing
December 10, 2012
Doesn't work in Chrome

Hi, Thanks for the tutorial. The given example works perfect in IE and Firefox; however, doesn't work in chrome. Thanks.