3

I'm calling a webpage via ajax. Part of it's response is a small block of XML.

I tried parsing it but jQuery only seems to find some of the nodes. For example:

<aaa>
   <text>bbb</text>
   <image>test</image>
</aaa>

It finds text just fine but never finds the image node.

But if I change the spelling from "image" to "zimage" it finds it. Is the word "image" reserved when parsing XML through jQuery?

My jQuery code is very simple...

$(data).find("zimage").each(function() {
    alert("node found");
});

That code works, but when I use this...

$(data).find("image").each(function() {
    alert("node found");
});

It never finds anything.

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
gshauger
  • 747
  • 4
  • 16
  • 2
    A small block of XML? Is this being treated as XML or do you mean the response is an HTML document with some invalid tags in it? – Quentin Mar 14 '11 at 20:59
  • The AJAX response consists of several delimited items...the last item is a small block of XML. – gshauger Mar 14 '11 at 21:01
  • What are you doing with the small block of XML before trying to run the selector engine over it? – Quentin Mar 14 '11 at 21:03
  • Nothing I literally tokenize the response then run the selector engine over the 3rd token which has the XML – gshauger Mar 14 '11 at 21:06
  • If you are tokenizing the response, then the last token (xml string) has to be converted to an XML DOM before querying it with jQuery. See these related questions [(1)](http://stackoverflow.com/questions/3152062/parse-content-like-xml-with-jquery), [(2)](http://stackoverflow.com/questions/3284477/best-way-to-parse-this-xml-with-jquery). As Vivin suggested, use `$.parseXML` if you're on 1.5. – Anurag Mar 14 '11 at 21:16

3 Answers3

3

What version of jQuery are you using? It appears that jQuery 1.5 has a parseXML() function:

var data="<aaa><text>bbb</text><image>test</image></aaa>";
var xmlDoc = jQuery.parseXML(data);
var $xmlDoc = jQuery(xmlDoc);

$xmlDoc.find("image").each(function() {
   alert("node found"); //this alert pops up because find() returns [image]
});

If you have control over the version of jQuery being used, you can try replacing it with version 1.5, which will give you access to the parseXML() function. It ooks like this function doesn't do any post-processing to the XML, and so you get a DOM that matches the XML. This way, you also don't need to be aware of what tags get modified, which means less special cases to handle.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
1

Because javascript is converting your "data" to this:

<aaa><text>bbb</text><img>test</aaa>

Consequently, this works:

var xml = "<aaa><text>bbb</text><image>test</image></aaa>";
var data = $("<div />", { html: xml });

data.find("img").each(function() {
    alert("node found");
});

Which is why it is better to use an XML library in almost all cases where you need to parse XML. You never know what quirks will popup.

mattsven
  • 22,305
  • 11
  • 68
  • 104
  • Is there a way I can output the XML in the form that jquery is converting it to? For my own debugging purposes? In other words, how did you obtain that string? Plus why is it converting it to begin with? thanks! – gshauger Mar 14 '11 at 21:04
  • Like this: alert( data.html() ); or alert( data[0].innerHTML ); – mattsven Mar 14 '11 at 21:06
1

You need to convert the xml into a DOM to be traversed by jQuery. jQuery doesn't work on xml directly but does great work through selection on the DOM provided by the browser.

Here is a plugin that returns a DOM for an XML string: http://outwestmedia.com/jquery-plugins/xmldom/

Brandon
  • 2,574
  • 19
  • 17