Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

AJAX related XML parsing

Posted on 2006-06-21
2
Medium Priority
?
267 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 2000 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

Technology Partners: 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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

876 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