Solved

AJAX related XML parsing

Posted on 2006-06-21
2
246 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now