2

I have a list of quotes in an XML document. Each quote is wrapped like this:

<Item>
    <Quote>This is a quote!</Quote>
    <Source>-- this is the Source of the Quote!</Source>
</Item>

Here's the jQuery:

    var html = '';
    var tmpl = '<li class=""><p class="quote">__quote</p><p class="source">__source</p></li>';

    $(quoteObj).find('Item').each(function(){ 

        $that = $(this);

        var _quote = $that.children('Quote').text();
        var _source = $that.children('Source').text();

        var qhtml = tmpl.replace('__quote', _quote).replace('__source', _source);

        html += qhtml;

    });

   return html;

In the end product, the QUOTES are all there, but the SOURCES aren't. I can't for the life of me figure out why. What's right in front of me that I can't see?

ADDITIONAL INFORMATION TO ANSWER COMMENTS:

  1. The XML is properly formed, and I changed it above.
  2. I added the var tmpl line to show what I'm replacing in the loop. The __quote is being replaced, and the __source is at least being acted upon, since the second <p> is empty, instead of containing a string.
  3. I have checked the actual XML coming back from the AJAX call, and it is all there, as it should be.

It seems to me this is some sort of issue with scoping and this, or with the action of the .children() method, but I still can't find it.

ONE LAST NOTE:

Changed the XML tag case to Initial Caps, which it is in the document in question.

8
  • 2
    Could you post the actual XML? Your sample is not well-formed (which could be the answer to your question, or could just be a shortcut you took to show us something quickly) Commented Nov 6, 2010 at 4:33
  • do you the values in the xml for Source. it should be sraight forward check , just hit the break point in firebut ,you can easliy check that. Commented Nov 6, 2010 at 4:49
  • @hollenback , just more info , check if you are getting results from backend through .net panel if you are getting results then add a breakpoint in the loop and check the values. Commented Nov 6, 2010 at 4:54
  • What is tmpl, and what is replace() ? If replace should be the native string-replace, this expects a RegExp as first argument, not a string. Commented Nov 6, 2010 at 7:05
  • 1
    @Dr.Molle, if by "native" string-replace you mean javascript's, that first argument can be a regexp or a string. Commented Nov 6, 2010 at 10:30

2 Answers 2

3

jQuery doesn't parse XML. Passing an XML string to $() simply assigns the string as the innerHTML property of an element, which has variable and unpredictable results. You need to parse the XML yourself, using the browser's built-in XML parser, and then pass the resulting document into jQuery:

var parseXml;

if (window.DOMParser) {
    parseXml = function(xmlStr) {
       return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
    parseXml = function(xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    parseXml = function() { return null; }
}


var xmlStr = "<Item><Quote>This is a quote!</Quote><Source>-- this is the Source of the Quote!</Source></Item>";

var xmlDoc = parseXml(xmlStr);
$xml = $(xmlDoc);

$xml.find('Item').each(function() {
    // Do stuff with each item here
    alert("Item");
});
Sign up to request clarification or add additional context in comments.

Comments

1
+50

Just tried this and the only thing I had to change was the find line to match the case of the XML node, eg

$(quoteObj).find('ITEM').each( function() {

I did also change the $that assignment line to include the var keyword, but it was working before I did that

var $that = $(this);

6 Comments

Were those the only change you made? Cos, it doesn't work with thos changes for me. Could you test it with the jQuery on google and see what result you get? Link to jQuery file from google : <SCRIPT type="text/javascript" src="ajax.googleapis.com/ajax/libs/jquery/1.4.2/…>
That's exactly what I'm using
Super weird. Wonder why I can get it to work with your changes? Care to post the complete example?
Yes it works when loading data from external file, but It does not work if you take xml in a local variable and then try to parse it using jquery.
jQuery doesn't parse XML; passing a string to the $() function merely assigns it as the innerHTML property of an HTML element, which is not at all a reliable to parse XML. You need to use the browser's built-in XML parser. See my answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.