Solved

AJAX related XML parsing

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

18 Experts available now in Live!

Get 1:1 Help Now