views:

123

answers:

2

I am having trouble parsing 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&lt;/url&gt;
        <img>1c9a871e2e074616ff45e26d8c2f7715.gif</img>
    </link>
    <link id="2">
        <title>test445</title>
        <url>http://yahoo.com&lt;/url&gt;
        <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');
      });

} }); });

A: 

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 = "" + title + "<\/a>" + "
";

});
}

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

cheers

jonathan p
A: 

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');
  });
David Glass