PDA

View Full Version : XML parsing with JS problems


Syntax-Error
01-29-2005, 01:02 AM
ok this is a little confusing but i'll to my best to explain.

I am making a completely client side, but very simple bloging script. It uses JavaScript to parse the XML, then assign the values of it to HTML elements. I have this part working fine. I am now attempting to create a form button that changes the blog entry to the one lower down in the xml file (as in a previous entry). This will make more since if you look at the files. I used a for next loop, and everything looks fine to me, but of couse, it doesnt work. Please take a look and see if you can find a problem. The html is attached to this post, and the xml file is below (can't attach .xml to a post). Thanks.

The XML:


<?xml version="1.0" ?>
<blog>

<note>
<to>third to</to>
<from>Third from</from>
<heading>Third heading</heading>
<body>Third body</body>
</note>

<note>
<to>Second to</to>
<from>second from</from>
<heading>second heading</heading>
<body>second body</body>
</note>

<note>
<to>first to</to>
<from>first from</from>
<heading>first heading</heading>
<body>first body</body>
</note>
</blog>

coothead
01-29-2005, 02:51 AM
Hi there Syntax-Error,

I am not really familiar with XML :o ....but with some
modifications, it now works - I.E. only, of course. :D
<html>
<head>
<title>Test></title>

<script type="text/javascript">

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("note.xml");

var entry = 0;

function note() {

to.innerText=xmlDoc.getElementsByTagName("to").item(entry).text;
from.innerText=xmlDoc.getElementsByTagName("from").item(entry).text;
header.innerText=xmlDoc.getElementsByTagName("heading").item(entry).text;
body.innerText=xmlDoc.getElementsByTagName("body").item(entry).text;

if(entry<2) {
entry++;
}
}
onload=note;

</script>

<body bgcolor="black">
<font color="white">
<h1>reading the XML</h1>
<b>To: </b>
<span id="to"> </span>
<br />

<b>From: </b>
<span id="from"></span>
<hr />

<b><span id="header"></span></b>

<hr />
<span id="body"></span>

</font>
<br />
<form action="#">
<div>
<input type="button" id="next" value="Previous>" onclick="note()"/>
</div>
</form>

</body>
</html>

Syntax-Error
01-29-2005, 11:04 AM
Thanks a lot dude! It works great. Im going to try to add another xml element that is the src of an image that will display in the header, then get the JS to put that url in the src of a waiting <img /> tag. Just forwarning you, this will likely go wrong. Thanks again!