For Developers

How to read XML using Javascript?

The Document Object Model (DOM) as implemented in MSXML provides a programmatic representation of XML documents, fragments, nodes, or node-sets. It also provides an application programming interface for working with XML data.

The following JScript fragments outline the basic process of programming with XML DOM.


var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");


Example:


function loadXML(xmlFilePath)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e)
{
alert(e.message)
}
}
try
{
xmlDoc.async=false;
xmlDoc.load(xmlFilePath); // xmlFilePath - xml file path Ex: test.xml
return(xmlDoc);
}
catch(e)
{
alert(e.message)
}
return(null);
}

0 comments:

Post a Comment