Solved

AJAX related XML parsing

Posted on 2006-06-21
2
247 Views
Last Modified: 2013-11-19
I'm working with AJAX for xml parsing pretty much for the first time, and I'm not really sure how to go about traversing the object returns from responseXML

ideally I would love some sort of traverser script were I can pass in the return from responseXML it will return an object something like this...


var myDoc = buildObjectFromXML(myXMLHttpRequest.responseXML);


then given this xml

<quote>
      <id>10</id>
      <customer>
            <name>
                  <first>Bill</first>
                  <last>Smith</last>
            </name>
      </customer>
      <inventory>
            <length>2</length>
            <item>
                  <name>A</name>
            </item>
            <item>
                  <name>B</name>
            </item>
      </inventory>
</quote>



I could make the following calls for their expected respective results...

myDoc.quote.id    //10
myDoc.quote.customer.name.first //Bill
myDoc.quote.inventory.length //2
myDoc.quote.inventory.item[1].name //B




If there is something like that I would love to know...


The other thing I could use is a really good examples of how to traverse this structure.

0
Comment
Question by:Isisagate
2 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 500 total points
ID: 16968918
Just noticed this Q - it's been around for a couple of days! That's pretty unusual. Anyway, I presume you know that processing the JS varies from browser to browser - Firefox handles it natively and IE uses an inbuilt ActiveX object.  You'll also want to make sure that you correctly process the return which may be in the text part of the response. So, to handle your XML, this is what you need to do:

function buildObjectFromXML(responseXML, responseText) {
  var xmlNode = null;
  if (responseXML) {
     xmlNode = responseXML.documentElement;
   }
   else if (responseText) {
      var startXML = responseText.indexOf("<?xml");
     
      var xmltxt = responseText.substring(startXML);

      var xmlDoc = null;
      if (document.implementation && document.implementation.createDocument) {
         var xmlParser = new DOMParser();
         xmlDoc = xmlParser.parseFromString(xmltxt, "text/xml");
      } else {
         xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
         xmlDoc.loadXML(xmltxt);
      }
      xmlNode = xmlDoc.documentElement;
   }
   return xmlNode;
}

So this gives you the starting point, which you'd use similar to what you had above:
   var myDoc = buildObjectFromXML(myXMLHttpRequest.responseXML, myXMLHttpRequest.responseText);

Now, to process this, you can traverse it using:
   for (int iX=0; iX<myDoc.childNodes.length; iX++)

Note that you usually have to test for the type of each node, i.e.
   if (myDoc.childNodes[iX].nodeType == <whatever>)

The node types are defined here, but you'll have to play with this stuff to see what you get. Use Firefox & its  excellent Venkman debugger to trace this
      1 Element
      2 Attribute
      3 Text
      4 CDATA
      5 Entity reference
      6 ?
      7 Processing instruction
      8 Comment
      9 Document
      10 Document type
      11 Document fragment
      12 Annotation
0
 
LVL 11

Author Comment

by:Isisagate
ID: 16969054
Actually the next day I found this link that was a great help and did exactly what I was hoping for...

http://www.kawa.net/works/js/xml/objtree-e.html

I will give you the points for responding.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question