开发者

Outputting content from an xml file using jquery

开发者 https://www.devze.com 2023-03-16 17:40 出处:网络
I\'m having some trouble with a jquery script I\'m writing, as I cannot get it to fetch the data I want.

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开发者_StackOverflow 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


The problem is in this line:

$(xml).find("Title":contains("O2 Brandroom"))

...specifically the colon after "Title", which is invalid Javascript syntax.

The intention of this code is for the contains() bit to be a part of the same selector as Title, so try putting the whole thing inside quotes:

$(xml).find("Title:contains('O2 Brandroom')")


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消