trojantiger
Technical User
Hi all,
Thought I would register here to ask a question as it seems this is an active forum.
I am trying to extract some info from an XML file. Here is a snippet of it:
Easy enough, right? And, I am using the following code to try to grab some info. I am working in the second function "buildMenus()".
My issue is this: I am able to find the XML tag names, such as <NAME> or <SHOW> or <TIMESLOT>, etc, with .nodeName, but changing to .nodeValue I am unable to get that information to show up. For example, x.x.x.nodeName = "NAME", but x.x.x.nodeValue = null, or is empty, not the "ABC" or "CBS" which I am expecting.
I have made some simple alert statements to simply show that I am able to find them, but all I get back is "null" or "" (nothing).
Any idea what I am doing wrong?
Thanks!
Thought I would register here to ask a question as it seems this is an active forum.
I am trying to extract some info from an XML file. Here is a snippet of it:
Code:
<TVLISTINGSRESULTS>
<CHANNELS>
<CHANNEL>
<NAME>ABC</NAME>
<SHOW>
<TIMESLOT>11:00pm - 1:05am</TIMESLOT>
<LINK>N/A</LINK>
<SHOWNAME>Local Programming</SHOWNAME>
</SHOW>
<SHOW>
<TIMESLOT> 1:05am - 5:00am</TIMESLOT>
<LINK>N/A</LINK>
<SHOWNAME>Local Programming</SHOWNAME>
</SHOW>
</CHANNEL>
<CHANNEL>
<NAME>CBS</NAME>
<SHOW>
<TIMESLOT>12:00am - 6:00am</TIMESLOT>
<LINK>N/A</LINK>
<SHOWNAME>Local Programming</SHOWNAME>
</SHOW>
</CHANNEL>
<CHANNEL>
<NAME>FOX</NAME>
<SHOW>
<TIMESLOT>12:00am - 6:00am</TIMESLOT>
<LINK>N/A</LINK>
<SHOWNAME>Local Programming</SHOWNAME>
</SHOW>
</CHANNEL>
</CHANNELS>
</TVLISTINGSRESULTS>
Easy enough, right? And, I am using the following code to try to grab some info. I am working in the second function "buildMenus()".
Code:
<html>
<head>
<title>Search Listings</title>
<script language="javascript">
<!--
function beginProgram()
{
var URL;
URL="file2.xml"; //which has the contents above from the XML snippet
if (window.ActiveXObject) //Checking if the browser is IE
{
//code removed to save space...
}
else if (document.implementation && document.implementation.createDocument)//for mozilla based browsers
{
var xmlDoc= document.implementation.createDocument("","doc",null); // NS
xmlDoc.async=false; //make sure doc is fully loaded
loaded = xmlDoc.load(URL);
if(!loaded)
{
alert("Error in XML file");
}
else
{
buildMenus(xmlDoc);
}
}
}
function buildMenus(XMLstuff) {
var tree = XMLstuff.documentElement; //documentElement = "TVLISTINGSRESULTS
var treenodes=tree.childNodes;
//alert(tree.childNodes[1].nodeName); = CHANNELS
//alert(tree.childNodes[1].childNodes[1].nodeName); = CHANNEL
alert(tree.childNodes[1].childNodes[5].childNodes[1].nodeValue);
//I can get the <CHANNEL>, <NAME>, <TIMESLOT> to show up
//but I cannot get alert() to show the contents between these tags!!
//which would be "ABC" for channel, "Local Programming" for name, or the time for the <TIMESLOT>.
document.write(tree.childNodes[1].childNodes[3].childNodes[5].childNodes.length);
for(i=0; i<tree.childNodes[1].childNodes[3].childNodes[5].childNodes.length;i++){
document.write(tree.childNodes[1].childNodes[3].childNodes[5].childNodes[i].nodeValue);
}
}
-->
</script>
</head>
<body onLoad="beginProgram()">
</body>
</html>
My issue is this: I am able to find the XML tag names, such as <NAME> or <SHOW> or <TIMESLOT>, etc, with .nodeName, but changing to .nodeValue I am unable to get that information to show up. For example, x.x.x.nodeName = "NAME", but x.x.x.nodeValue = null, or is empty, not the "ABC" or "CBS" which I am expecting.
I have made some simple alert statements to simply show that I am able to find them, but all I get back is "null" or "" (nothing).
Any idea what I am doing wrong?
Thanks!