Help parse this data with jquery xml parsing

I am having trouble processing certain data using jquery. I tried several tutorials and samples on the Internet, but I didn't seem to have any luck with the way my data was 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 can I extract 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');
      });

}}); });   

+3
source share
2 answers

try the following:

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');
  });
+1
source

try it -

   $(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 around with attributes a bit, but this will work defo. I'm doing something like that.

greetings

0
source

Source: https://habr.com/ru/post/1730461/


All Articles