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

Best way of displaying XML data?

Status
Not open for further replies.

simon373

Programmer
Oct 25, 2006
25
0
0
Hi,

I have a project where I need to retrieve data on animals from an XML file. The XML file might look like:

<?xml version="1.0"?>
<animals>
<cat>
<headertext>
Animal Facts - Cat
</headertext>

<descriptiontext>
A cat is a an intelligent animal.
</descriptiontext>

<photourl>
../images/cat.jpg
</photourl>

<gameurl>
../games/catgame.swf
</gameurl>

</cat>
</animals>

Obviously, there will be more animals within it.

I have loaded the XML data into my web page using the following javascript:

<script type="text/javascript">
var request = new XMLHttpRequest();
request.onreadystatechange = myStatusProc;
request.open( "GET", "data/animalinfo.xml", true );
request.send();

function myStatusProc()
{
if (request.readyState == 4)
{
if(request.status == 200)
{
var response = request.responseXML.documentElement;


}
}
}

So, lets say I open a the cat web page, how do I retrieve and display information from specific nodes within the cat element?

Is there an easier way of doing this, with JSON? I am familiar with PHP and know how I would do it using PHP, but I would like to improve my Javascript / XML knowledge!

Many thanks, Simon.
 
Using the latest version of the Prototype JS framework (1.6.0), you'd not only get a nice set of AJAX libraries, but you get access to a ready-made "responseJSON" object which you can use immediately without having to parse / eval it...

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
For instance.
[tt]
var s;
s=response.getElementsByTagName("descriptiontext")[0].firstChild.data;
[/tt]
To:eek:p Don't let all the AJ**, JA clouds blind your way. They are for, well,...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top