Tuesday, March 10, 2009

Loading an XML file with Javascript.

Should fill the data from the XML file into the span tag on the HTML document.

<script type="text/javascript">
function parseXML()
{
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);
return;
}
}
xmlDoc.async=false;
xmlDocVal = "/xmlfile.xml";
xmlDoc.load(xmlDocVal);
document.getElementById("divtarget").innerHTML=
xmlDoc.getElementsByTagName("xmlnodesource")[0]
.childNodes[0].nodeValue;

XML File
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xmlnodesource>Data</xmlnodesource>

HTML
<span id="divtarget"></span>

No comments:

Post a Comment