Click to See Complete Forum and Search --> : Fetch data from .xml?


zheeka
08-25-2010, 10:45 AM
Hello folks!

Sorry, I'm a noob, and despite going through a tutorial about the use of .xml, I'm just tapping blindly in the dark....
I have a simple (i think..) .xml script containing data about a song that is now being played on a radio station, from wich I have to "extract" the artist and the song title.

The file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Schedule System="Jazler">
<Event status="happening" startTime="14:33:38" eventType="song">
<Announcement Display="Now On Air:"/>
<Song title="Stranger in Moscow(Tee's in-house club mix)">
<Artist name="MICHAEL JACKSON">
</Artist>
<Jazler ID="xxxxx"/>
<PlayLister ID=""/>
<Media runTime="00:06:41"/>
<Expire Time="14:40:18"/>
</Song>
</Event>
</Schedule>

I've tryed with a script looking like this:
<html>
<body>
<p><b>Artist:</b> <span id="artist"></span><br />
<b>Pjesma:</b> <span id="song"></span><br />

<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","NowOnAir.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

document.getElementById("song").innerHTML=
xmlDoc.getElementsByTagName("song")[0].childNodes[0].nodeValue;
document.getElementById("artist").innerHTML=
xmlDoc.getElementsByTagName("artist")[0].childNodes[0].nodeValue;

</script>

</body>
</html>

What am I doing wrong?

Thnx in advance!

Charles
08-26-2010, 08:39 AM
Perhaps it would be easier, and certainly more broadly supported, if you simply served the XML with an XSLT stylesheet.

sohguanh
08-27-2010, 04:20 AM
Hello folks!

Sorry, I'm a noob, and despite going through a tutorial about the use of .xml, I'm just tapping blindly in the dark....
I have a simple (i think..) .xml script containing data about a song that is now being played on a radio station, from wich I have to "extract" the artist and the song title.

The file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Schedule System="Jazler">
<Event status="happening" startTime="14:33:38" eventType="song">
<Announcement Display="Now On Air:"/>
<Song title="Stranger in Moscow(Tee's in-house club mix)">
<Artist name="MICHAEL JACKSON">
</Artist>
<Jazler ID="xxxxx"/>
<PlayLister ID=""/>
<Media runTime="00:06:41"/>
<Expire Time="14:40:18"/>
</Song>
</Event>
</Schedule>
What am I doing wrong?

Thnx in advance!

Recently in my workplace I have the opportunity to play with AJAX and process responseXML coming back. Let me tell you, for reasons unknown, when you use those DOM getElementsByTagName etc... they process the XML content like a "single" level.

E.g
You would typically write code to iterate through the children of Song tag to get to their children etc which we are familiar but it is NOT!

I spend a lot of time with my colleague and then discover, it process XML tags like a "single" level

E.g
if you get getElementsByTagName("song"), you retrieve all the values for that tag.

if you get getElementsByTagName("Artist"), you retrieve all the values for that tag.

etc etc

Think of it as processing the XML like a "single" level. So strange isn't it ? Hmmm..... strange but true.