开发者

help parse this data using jquery's xml parsing

I am having trouble parsing开发者_JS百科 specific data using jquery. I have tried a few tutorials and samples online but I don't seem to have any luck with the way my data is formatted.

If I have xml data as follows:

<links total="2">
    <link id="1">
        <title>whatwhat</title>
        <url>http://google.com</url>
        <img>1c9a871e2e074616ff45e26d8c2f7715.gif</img>
    </link>
    <link id="2">
        <title>test445</title>
        <url>http://yahoo.com</url>
        <img>3233c3b40f8db13274c21b6f78f04d06.gif</img>
    </link>
</links>

How would I go about extracting all the elements?

So far I tried this to get the id but it did not work:

<script>
    $(document).ready(function(){
     $.ajax({
  type: "GET",
  url: "http://mysite.com/where/data.xml",
  dataType: "xml",
  success: function(xml) {
      $(xml).find('link').each(function(){
   var id = $(this).attr('id');
   $('<div class="items" id="link_'+id+'"></div>').html('<a href="#">link</a>').appendTo('#page-wrap');
      });

} }); });


try this:

success: function(xml) {
  $(xml).find('link').attr('id').each(function(){
    var id = $(this).text();
    $('<div class="items" id="link_'+id+'"></div>').html('<a href="#">link</a>').appendTo('#page-wrap');
  });


try this -

   $(document).ready(function(){
     $.ajax({
  type: "GET",
  url: "http://mysite.com/where/data.xml",
  dataType: "xml",
  success: parseXml
      });

function parseXml (xml) {
    $(xml).find("entry").each(function()
    {
    var $item = $(this);
    var title = $item.find("title").text();
    var link = $item.find("url").text();
    var output = "<a href=\"" + link + "\" target=\"_self\">" + title + "<\/a>" + "<br />";

    });
    }

you may need to play with attributes a bit but this will defo work i am doing something similar.

cheers

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜