Well, this issue is making me mad. I think that I have a lack of knowledge in this aspect.
I am trying to parse a XML response, everything is ok but the problem is when I try to access to objects. I treat them like arrays and I always receive “undefined”:
<?xml version="1.0" encoding="UTF-8" ?>
<ajax-response>
<response>
<item>
<name><![CDATA[ok]]></name>
<value><![CDATA[true]]></value>
</item>
<item>
<name><![CDATA[menuDiv]]></name>
<value><![CDATA[Some HTML value]]></value>
</item>
</response>
</ajax-response>
And here is the Jquery code:
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$item = $xml.find( "item" );
alert($item.length);
$item.each(function(key, value){
alert(typeof value);
});
This line alert(typeof value); returns “object”. However, if I do value[0] or $value[0] it returns “undefined”.
I would like to get “Some HTML value” from the object.
The
valueobject passed into your iteration function will be an XML DOM node, in your case anElementwith the tag nameitem. So you’d retrieve information from it by getting attributes from it, or looking at the text in any childTextNodes that it has.You can wrap the element in a jQuery instance and use jQuery’s
attrto get attributes, andtextto get text.That would be
text— here’s an example:Here’s a full example that loops through the children of each
item: Live copy | sourceOr if you just want to grab the
valuethat’s the sibling of thenamecontaining the text"menuDiv", the relevant bit is:Live example | source