0

I can't figure this out and been messing with it for a while now.

XML:

<page name="bio">
<image imageUrl="img/bio.png" link="#"/>
<texts>
<pageText><![CDATA[blah
]]></pageText>
</texts>
</page>

jQuery

$.get('../tfile_main.xml', function(xml){
$varbioimg = $(xml).find('page[name="bio"] image');
alert($varbioimg.attr('imageUrl'));
});

this returns undefined for some reason , I also tried to find('page[name="bio"] > image')

2 Answers 2

3

try .parseXML, using jQuery's css selectors to parse xml is not recommended

var xml = '<page name="bio"><image imageUrl="img/bio.png" link="#"/><texts><pageText><![CDATA[blah]]></pageText></texts></page>';

xmlDoc = $.parseXML( xml );
$xml = $( xmlDoc );    
console.log($xml.find('page[name="bio"] image'));

here is the demo http://jsfiddle.net/FVHEU/

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, can parseXML() be used with an external xml file though?
if that file is read by the server and sent as xml to the client side then yes it can be used on the external file...
0

While I second using .parseXML, I also have to mention that your original example worked for me. Make sure that your XML file exactly matches what you've posted here (with the addition of <?xml version="1.0"> at the top) and navigate to the URL of the XML file with Firefox; it will let you know if there are any parsing errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.