Parsing XML with JavaScript

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

Lets assume that I want to parse the following XML with JavaScript:

fruit.xml:

XML Code: [ Select ]
<fruit>
 <apple>red</apple>
 <banana>yellow</banana>
 <orange>orange</orange>
 <citrus>
   <orange>orange</orange>
   <lemon>yellow</lemon>
 </citrus>
</fruit>
 
  1. <fruit>
  2.  <apple>red</apple>
  3.  <banana>yellow</banana>
  4.  <orange>orange</orange>
  5.  <citrus>
  6.    <orange>orange</orange>
  7.    <lemon>yellow</lemon>
  8.  </citrus>
  9. </fruit>
  10.  


And I use the following JavaScript:

JAVASCRIPT Code: [ Select ]
 
      var request = GXmlHttp.create();
      request.open('GET', 'fruit.xml', true);
      request.onreadystatechange = function() {
      if (request.readyState == 4 && request.status == 200) {
      var xmlDoc = request.responseXML;
      var xmlrows = xmlDoc.documentElement.getElementsByTagName("fruit");
     
     
      for (var i = 0; i < xmlrows.length; i++) {
         var xmlrow = xmlrows[i];
         
         var Xmlapple = xmlrow.getElementsByTagName("apple")[0];
         var Textapple = Xmlapple.firstChild.data;
                        document.write(Textapple);
 
         var Xmlbanana = xmlrow.getElementsByTagName("banana")[0];
         var Textbanana = Xmlbanana.firstChild.data;       
         document.write(Textbanana);
      }
 
  1.  
  2.       var request = GXmlHttp.create();
  3.       request.open('GET', 'fruit.xml', true);
  4.       request.onreadystatechange = function() {
  5.       if (request.readyState == 4 && request.status == 200) {
  6.       var xmlDoc = request.responseXML;
  7.       var xmlrows = xmlDoc.documentElement.getElementsByTagName("fruit");
  8.      
  9.      
  10.       for (var i = 0; i < xmlrows.length; i++) {
  11.          var xmlrow = xmlrows[i];
  12.          
  13.          var Xmlapple = xmlrow.getElementsByTagName("apple")[0];
  14.          var Textapple = Xmlapple.firstChild.data;
  15.                         document.write(Textapple);
  16.  
  17.          var Xmlbanana = xmlrow.getElementsByTagName("banana")[0];
  18.          var Textbanana = Xmlbanana.firstChild.data;       
  19.          document.write(Textbanana);
  20.       }
  21.  


This works fine and dandy when parsing the apple & banana nodes. But if I want to parse the orange node it only parses the orange node found with in the fruits node. How do I parse the orange node found inside the citrus node? I tried running another loop inside the existing loop to grab the citrus node first, but that didn't work. Have any ideas?

And, BTW GXmlHttp is part of the Google Maps API, just ignore it.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

You could use XPath to get it, but for starters, I don't see any code there selecting anything but apples and bananas. You shouldn't get any orange results at all. Secondly, if it looks the same as the other two, the [0] is useless. After that, I think you're specifying that you only want the FIRST match when you say firstChild, so that's what you get.

That being said, IMO you should really consider selecting elements into groups and parsing from there or following node paths (XPath is a possibility here, too).

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 60 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.