...

View Full Version : JS to Parse XML doc



hothousegraphix
01-01-2007, 07:29 PM
I'm have trouble writing a proper "for loop" to parse my XML doc and write the info grabbed to my html doc.

If I simply spell out exactally what I want the info writes as intended; e.g.

document.getElementById("date_1").innerHTML=xmlDoc.getElementsByTagName("date")[0].childNodes[0].nodeValue;
document.getElementById("copy_1").innerHTML=xmlDoc.getElementsByTagName("copy")[0].childNodes[0].nodeValue;

document.getElementById("date_2").innerHTML=xmlDoc.getElementsByTagName("date")[1].childNodes[0].nodeValue;
document.getElementById("copy_2").innerHTML=xmlDoc.getElementsByTagName("copy")[1].childNodes[0].nodeValue;
But, when I try to consolitdate the above into a loop nothing writes. My XML doc is streight forward; e.g.
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
- <news>
- <entry>
<date>Entry for 11/8/2006</date>
<copy>Some Text Here</copy>
</entry>
- <entry>
<date>Entry for 11/1/2006</date>
<copy>Some Text Here</copy>
</entry>
- <entry>
<date>Entry for 11/1/2006</date>
<copy>Some Text Here</copy>
</entry>
</news>
And my xhtml is as follows; e.g.

<body onload="loadXML()">
<p><span id="date_1"></span><br />
<span id="copy_1"></span>
</p>
<p><span id="date_2"></span><br />
<span id="copy_2"></span>
</p>
</body>
I've tried a number of things but have had no success. Here is what I'm currently working with:

function getMessage()
{
for ( i=0; i < xmlDoc.length; i++ )
{
var entryDate[i] = xmlDoc.getElementsByTagName("date")[i].childNodes[0].nodeValue;
var entryCopy[i] = xmlDoc.getElementsByTagName("copy")[i].childNodes[0].nodeValue;

document.getElementById("date_" + (1 + i)).innerHTML = entryDate[i];
document.getElementById("copy_" + (1 + i)).innerHTML = entryCopy[i];
}
}
The above is not working. Would anyone have some advice on how to correct?

Thanks!

hothousegraphix
01-02-2007, 03:59 AM
Anyone?

david_kw
01-02-2007, 05:57 AM
If you are saying your first one works and your second doesn't then I'd guess something is wrong in the intermediate variables. I'm not sure what

var entryDate[i] = ...

does since it's not really an array but also not a valid variable name (of course javascript seems to allow some wacky code so who knows). Try just

var entryDate = ...

If that doesn't work try giving a link to an example that doesn't work because I don't see anything obviously wrong with what you are doing other than the intermediate variable name in your loop.

david_kw

Ancora
01-02-2007, 11:35 AM
hothousegraphix:

This works, using your .xml file, named: news.xml ---



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Any Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

var AdminResponse = "";

function parseSettings(){

var nDate = AdminResponse.getElementsByTagName('date');
var nCopy = AdminResponse.getElementsByTagName('copy');
for (i=1; i<nDate.length+1; i++)
{
document.getElementById('date'+i).innerHTML = nDate[i-1].firstChild.data;
document.getElementById('copy'+i).innerHTML = nCopy[i-1].firstChild.data;
}
}

function getNews(){

var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
AdminRequest.onreadystatechange = function()
{
if (AdminRequest.readyState == 4)
{
if (AdminRequest.status == 200)
{
AdminResponse = AdminRequest.responseXML;
parseSettings();
}
else {
alert('Error news.xml File '+ AdminRequest.statusText);
}
}
}
var forceGET = "?n="+ parseInt(Math.random()*999999999);
AdminRequest.open("GET", "news.xml"+forceGET, true);
AdminRequest.send(null);
}

onload=getNews;

</script>
<style type="text/css">

body {background-color:#eae3c6;margin-top:60px}

</style>
</head>
<body>
<div id='date1'></div>
<div id='copy1'></div>

<div id='date2'></div>
<div id='copy2'></div>

<div id='date3'></div>
<div id='copy3'></div>
</body>
</html>




<?xml version="1.0" ?>
<news>
<entry>
<date>Entry for 11/8/2006</date>
<copy>Some Text Here</copy>
</entry>
<entry>
<date>Entry for 11/1/2006</date>
<copy>Some Text Here</copy>
</entry>
<entry>
<date>Entry for 11/1/2006</date>
<copy>Some Text Here</copy>
</entry>
</news>

hothousegraphix
01-03-2007, 08:30 PM
Sorry for not responding sooner.

Ancora - I appreciate you offering a complete solution to my situation. Iíve tested and all works as intended.

One question, the option here displays the imported content without placing it in the mark-up. Do you have a preference for whether or not the imported information writes to the document?

hothousegraphix
01-03-2007, 08:32 PM
Sorry for not responding sooner.

Ancora - I appreciate you offering a complete solution to my situation. Iíve tested and all works as intended.

One question, the option here displays the imported content without writing it to the mark-up. Do you have a preference for whether or not the imported information writes to the document?

Ancora
01-03-2007, 08:42 PM
hothousegraphix:

You're welcome. I appreciate your courtesy. It's a rarity here.

I don't understand. The code I posted is a complete .html document, and when tested with the news.xml document, inserts the relevant text in to the divs.

I don't know what you mean by:
"preference for whether or not the imported information writes to the document?"

hothousegraphix
01-04-2007, 03:51 AM
Ahhh.

I probably should have been more clear. Yes, the content from news.xml is dynamically inserted in to the div's. However, the script does not write the dynamically inserted text to the page source itself. I'm not at all even sure that there would be a benefit to having that occur.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum