Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Reading an XML Response

    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!
    Last edited by izecul; 01-05-2011 at 10:58 PM.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by izecul View Post
    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).

    Quote Originally Posted by izecul View Post
    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.
    Last edited by Dormilich; 01-06-2011 at 06:53 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    izecul (01-08-2011)

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by Dormilich View Post
    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.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • Users who have thanked rnd me for this post:

    izecul (01-06-2011)

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by rnd me View Post
    if anything, i would say JSON need not be parsed, JSONp for example.
    care to prove?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by Dormilich View Post
    care to prove?
    html:
    Code:
    <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:

    Code:
    <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...
    Last edited by rnd me; 01-06-2011 at 07:48 PM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    I see a lot of hard coded JSON.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by Dormilich View Post
    I see a lot of hard coded JSON.
    what does that mean?
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #8
    New Coder
    Join Date
    Jan 2011
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    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)
    Code:
    <?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?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •