Hi,
I am trying to parse an XML file in order to read data from the XML file. I am using an Xpath expression in javascript in order to parse the XML file. Everything works fine in INternet Explorer where I manage to parse the XML file and get back an answer from this file, however I am having problems doing so in Firefox. I am not able to get any type of answer from the XML file when I use Firefox.
This is my code in javascript:
<script type="text/javascript">
var searchString = document.location.search;
searchString = searchString.substring(1);
var nvPairs = searchString.split("&");
for (i = 0; i < nvPairs.length; i++)
{
var nvPair = nvPairs[i].split("=");
var name = nvPair[0];
var value = nvPair[1];
if (i == 0) {
var time = value;
}
if (i == 1) {
var type = value;
}
if (i == 2) {
var soil = value;
}
}
function loadXMLDoc(fname)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.X
MLDOM");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.cr
eateDocume
nt)
{
xmlDoc=document.implementa
tion.creat
eDocument(
"","",null
);
}
else
{
alert('Your browser cannot handle this script');
}
xmlDoc.async=false;
xmlDoc.load(fname);
return(xmlDoc);
}
xml=loadXMLDoc("Crops to grow.xml");
path="/Start/month[@on='" + time + "']/product[@id='" + type + "']/land[@type='" + soil + "']/grow";
// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path
);
for (i=0;i<nodes.length;i++)
{
document.write("PRODUCTS TO GROW: ",nodes[i].childNodes[0].n
odeValue);
document.write("<br /> <br />");
}
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.cr
eateDocume
nt)
{
var nodes=document.evaluate(pa
th, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext()
;
while (result)
{
document.write(result.chil
dNodes[0].
nodeValue)
;
document.write("<br />");
result=nodes.iterateNext()
;
}
}
</script>
It appears that the code which has to do with the Firefox browser is being neglected by javascipt!! Could someone please help me to identify the problem.
Thanks
Start Free Trial