Friday, October 17, 2008

How to read Xml file using java script?

Microsoft has provided an activex object for this functionality that is to load and read and xml file using Internet explorer.

It is "Microsoft.XMLDOM".
Example:

var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("ImageDescription.xml");
var Root=xmlDoc.documentElement;

Initially we have to create instance to that activex object, we can set the process as asynchrous while loading the xml onto client memory.
Later we need to sepcifcy the xml file name located at local path to the application or the html file.
Now this instance through which we have created the xml dom object , can be used to read elements using tag names or attribute etc... from the loaded xml file (ImageDescription.xml).


for(var rt=0;rt<Root.childNodes.length;rt++)
{
if(Root.childNodes[rt].nodeName=='Test node name')
{
.....
So using this document element object (Root), we can iterate through the xml tree nodes , and perform logical actions.

No comments: