...

View Full Version : Reading an XML Response



izecul
01-05-2011, 09:55 PM
Hi,
I am a newbie at XML and this issue is proving to be a tough nut to crack for me.
My AJAX query returns me an XML response of the following structure for a query on "ind":
<countries>
<country>
<name>India</name>
<capital>New Delhi</capital>
</country>
<country>
<name>Indonesia</name>
<capital>Jakarta</capital>
</country>
</countries>

How do I transform the XML response into a HTML layout? Particularly, how do I get down to the value at each node?How do I traverse the XML document using JS?

Is this (using XML) better than JSON (other than the obvious platform-independence)? Which is better?

Thanks!

Dormilich
01-06-2011, 05:50 PM
How do I transform the XML response into a HTML layout? Particularly, how do I get down to the value at each node?How do I traverse the XML document using JS?
you use JavaScript as usual, with the sole exception that you can not use HTML methods (only DOM methods* are available on XML).


Is this (using XML) better than JSON (other than the obvious platform-independence)? Which is better?
That depends on the script. the advantage of XML is that you don’t need to parse it to use it, the advantage of JSON is that after parsing you get native JavaScript objects (with XML you get DOM objects).


* - getElementById() only works when there are ID type attributes defined in the XML’s DTD.

rnd me
01-06-2011, 06:18 PM
the advantage of XML is that you don’t need to parse it to use it, the advantage of JSON is that after parsing you get native JavaScript objects (with XML you get DOM objects).

if anything, i would say JSON need not be parsed, JSONp for example. XML always needs loaded and parsed using some browser tack-on to javascript be it activeX, ajax, or otherwise.

JSON is much easier to process using javascript. xml proccessing involves painful and long-winded dom code, namespaces are frustrating, and there's no standard way to get xml back into a string.

JSON can use functional array methods, is easier to serialize, and performs MUCH faster than xml.

Dormilich
01-06-2011, 06:33 PM
if anything, i would say JSON need not be parsed, JSONp for example.
care to prove?

rnd me
01-06-2011, 06:42 PM
care to prove?

html:

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/services/search/web?v=1.0&callback=alert&q=obama'></script>

i see a parsed object, do you see parser code?


a more practical example of turning remote data into html:


<html>
<script type='text/javascript'>

function incoming(ob){
alert(
ob.responseData.results.map(function(a){
return a.titleNoFormatting.link(a.url);
}).join("\n\n")
);
}
</script>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/services/search/web?v=1.0&callback=incoming&q=obama'></script>
</html>
try that with xml...

Dormilich
01-06-2011, 06:45 PM
I see a lot of hard coded JSON.

rnd me
01-06-2011, 06:51 PM
I see a lot of hard coded JSON.

what does that mean?

izecul
01-08-2011, 07:40 PM
you use JavaScript as usual, with the sole exception that you can not use HTML methods (only DOM methods* are available on XML).

* - getElementById() only works when there are ID type attributes defined in the XML’s DTD.

What are the DOM methods? I tried using them to traverse an XML response but kept getting errors - I would get DOM objects but not their contents (which would be null or undefined)


<?xml version=1.0 ?>
<countries>
<country>
<name>India</name>
<capital>New Delhi</capital>
</country>
<country>
<name>UK</name>
<capital>London</capital>
</country>
<country>
<name>US</name>
<capital>Washington, DC</capital>
</country>
</countries>


If this is the response for my AJAX request, how do I separate it so that I can access the 'name' and 'capital' contents?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum