I’m having some trouble with a jquery script I’m writing, as I cannot get it to fetch the data I want.
I have an xml file of the following sort of layout (I’m presenting a fragment only),
<page1>
<title>First page</title>
<description>Lorem ipsum</description>
<image1 thumb='#'>images/image01.jpg</image1>
<image2 thumb ='#'>images/image02.jpg</image2>
</page1>
<page2>
<title>Second page</title>
<description>Lorem ipsum</description>
<image1 thumb='#'>images/image01.jpg</image1>
<image2 thumb ='#'>images/image02.jpg</image2>
</page2>
Now I want to find a way of fetching the description depending on the title value. My code is as folows and I can tell that something is horribly wrong.
$(document).ready(function() {
$.ajax({
type: "GET",
url: "xml_concept.xml",
dataType: "xml",
success: parseXml
});
});
function parseXml(xml) {
$(xml).find("title":contains("First Page")) {
$("#text-area").append('<div class="test-xml">' + $(this).text() + '</div>');
}
}
I’m pretty sure that the this is not referring to what I want, but rather the xml as a whole? If correct, how can I select the content of the tags I’m after and append them? I have been able to parse content successfully but not with the sort of conditions I’m setting, so I was thinking of using if statements or case/switch.
Another thing, am I right in assuming that I’ve used the ‘:contains’ totally wrong?
Your help is much appreciated
I found a more suitable way of achieving the results I desired by loading external html content direct instead of relying on one xml doc.