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

Parsing an XML file using javascript

Status
Not open for further replies.

trent101

Programmer
Nov 4, 2005
50
AU
Hey,

I need to display the contents of a xml file in a webpage using java script. This worked fine when My text was placed inside the <body> tag. However, when I go to insert the text inside a table the xml data will not be displayed. Is there a rule where you cannot display xml data inside a table? I have pasted the contents of my working html file below, not using tables. If anyone can explain why It wont let me use tables that would be appreciated.

thanks...

<html>
<head>

<script language="JavaScript"
for="window" event="onload">

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

title1.innerText = xmlDoc.childNodes(0).childNodes(0).childNodes(6).childNodes(0).text
description1.innerText = xmlDoc.childNodes(0).childNodes(0).childNodes(6).childNodes(1).text
link1.innerText = xmlDoc.childNodes(0).childNodes(0).childNodes(6).childNodes(2).text
category1.innerText = xmlDoc.childNodes(0).childNodes(0).childNodes(6).childNodes(3).text
pubdate1.innerText = xmlDoc.childNodes(0).childNodes(0).childNodes(6).childNodes(4).text

</script>

<title>XML Example</title>
</head>

<body bgcolor="white">
<h1>RFID News Feed</h1>

<b>Title: </b><span id="title1"></span>

<br>
<b>PubDate: </b><span id="pubdate1"></span>

<br>
<b>Desc: </b><span id="description1"></span>

<br><b>Link: </b><span id="link1"></span>

<br>Category: </b><span id="category1"></span>

<hr>


</body>
</html>
 
>[tt]title1.innerText = xmlDoc.childNodes(0).childNodes(0).childNodes(6).childNodes(0).text [/tt]

[tt]title1.innerText = xmlDoc.[red]documentElement.[/red]childNodes(0).childNodes(0).childNodes(6).childNodes(0).text [/tt]

etc etc... Furthermore, it is for ie only as you call the element by it's id directly and that you use innerText. With that correction, it would surely equally applicable to elements embedded within a table or not. The only condition is that they must support innerText property. (But you said the script as such is already working, even though I doubt it is working correctly, I won't insist.)
 
Tables or no tables, makes no difference. It's probably not working because the javascript is not referencing the HTML elements correctly or maybe innerText will not work with table elements. The way you have done it, eg:

title1.innerText

is IE specific. You should use:

document.getElementById('title1').nodeValue

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top