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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML/DOM help needed

Status
Not open for further replies.

trojantiger

Technical User
Oct 20, 2008
1
US
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:

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!
 
[0] >Easy enough, right?
Don't believe it if somebody tells you. Easy only if you understand it.

[1] I see you keep using childNodes[1] etc..., you are testing on moz-based browser. I can show you how.

[1.1] Let's take what you get as CHANNEL.
//alert(tree.childNodes[1].childNodes[1].nodeName); = CHANNEL
The fragment is this.
[tt]
<CHANNEL>[yellow]
xxxxxxxxxxxxx[/yellow][blue]<NAME>ABC</NAME>[/blue]
<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>
[/tt]
I put an "x" where it is blank.

Now in moz, CHANNEL's childNodes[0] is yellow, childNodes[1] is blue. That's how it is counted in moz ([red]not so in ie[/red]).

So to get to the name tag, it would be this.
[tt] tree.childNodes[1].childNodes[1].childNodes[1][/tt]
and the fragment is this.
[tt]
<NAME>[yellow]ABC[/yellow]</NAME>
[/tt]
Now the yellow part is the childNodes[0] of it. Hence, the nodeValue of it is referenced by this.
[tt]
alert(tree.childNodes[1].childNodes[1].childNodes[1].[blue]childNodes[0].nodeValue[/blue])
[/tt]
and you'll get "ABC".

[2] I would suggest you use getElementsByTagName() method which may be more easy to handle cross-browser even for a novice. The same "ABC" would be obtained by this.
[tt]
alert(tree.getElementsByTagName("NAME")[0].firstChild.nodeValue); //firstChild another name for childNodes[0]
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top