Solved

AJAX related XML parsing

Posted on 2006-06-21
2
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

630 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