Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

help parse this data using jquery's xml parsing

Tags:

jquery

xml

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

} }); });

like image 325
Eduardo Avatar asked Jul 15 '26 13:07

Eduardo


1 Answers

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');
  });
like image 117
David Glass Avatar answered Jul 18 '26 08:07

David Glass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!