Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript and xml

Status
Not open for further replies.

ice78991

Programmer
Nov 20, 2006
216
Trying to get a value from a .xml file using javascript but it is not returning any value. What should i be doing?

function importXML()
{
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = setDetails;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) setDetails()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}

xmlDoc.load("info.xml");
}

function setDetails(){

alert(xmlDoc.childNodes[0].childNodes[0].nodeValue);

}

importXML();



Here is my xml file

<info>
<numberofimages>2</numberofimages>
</info>
 
Ice, try on your alert statement to put
Code:
alert(xmlDoc.xml);
Just see if you are actually loading the document.

<.
 
It's coming up as undefined.

The .xml document is in the correct path etc.Is there anything in the code I should change?
 
I copied your code verbatim and put alert(xmlDoc.xml) and got the xml file. The file is not getting loaded up somehow. It could be as simple as case sensitivity in the XML filename.

Since I'm not sure what is going on there, I can say that when you get past that, you need to put this as your test alert
Code:
alert(xmlDoc.childNodes[0].childNodes[0].childNodes[0].nodeValue);

<info> <---xmlDoc.childNodes[0]
<numberofimages> <---xmlDoc.childNodes[0].childNodes[0]
2 <---xmlDoc.childNodes[0].childNodes[0].childNodes[0]

</numberofimages>
</info>

The 2 is a textNode and you must point to that node before you can extract the value from it.

<.
 
I've tried your suggestion of
alert(xmlDoc.xml) in ie7 and found that it works there ( but not in Firefox 1.5 which I have been using so far) However, even in ie 7 alert(xmlDoc.childNodes[0].childNodes[0].nodeValue); simply does nothing.
I also tried alert(xmlDoc.childNodes[0].childNodes[0].childNodes[0].nodeValue); which also returns nothing.

However, alert(xmlDoc.childNodes[0].nodeValue); returns a box which gives version and encoding info.

I am running this on a local machine so I tried it online. Same thing although I got a javascript error message
'Object Required'
in char 1 of the line
alert(xmlDoc.childNodes[0].childNodes[0].nodeValue);
 
Strange, I've been doing all this in IE6 and it works fine. I think I may know something try:
Code:
alert(xmlDoc.childNodes[1].childNodes[0].childNodes[0].nodeValue);
I am also running this on a local machine. I'm guessing that you have a version tag at the very top of your XML file. I just copied the exact code you listed without an <xml version> tag at the top. So (xmlDoc.childNodes[1]) will start you out in your root tag <info>.

<.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top