1

I tried to loop list items from XML using js. But the list data do not repeat with the bullets points.

Here is my code.

data.xml

<?xml version="1.0"?>
<paintings>
<cd>
  <para>posuere lacus in, accumsan nulla.</para>
  <list>Cras dolor dui hendrerit eget eleifend eu</list>
  <list>Cras dolor dui hendrerit eget eleifend eu</list>
  <list>Cras dolor dui hendrerit eget eleifend eu</list>
</cd>
</paintings> 

script.js

 $(document).ready(function(){
    $.ajax({
        type:"GET",
        url:"data.xml",
        dataType:"xml",
        success:xmlParser
    });
});

function xmlParser(xml){
    xml = $(xml).children();
    $(xml).children().each(function () {    

        let para = '<div>' + $(this).find("para").text() + '</div>';                
        let list = '<div>' + $(this).find("list").text() + '</div>';

        let html = `
        <p>${para}</p>
        <ul><li>${list}</li></ul>
        `;
        $("#xmldata").append(html);

    });
}

html

<div class="row" id="xmldata"></div>
Omid Nikrah
  • 2,444
  • 3
  • 15
  • 30
Hal Abelson
  • 89
  • 1
  • 8

3 Answers3

1

Try $.each and $.find

var xml = '<?xml version="1.0"?><paintings><cd>  <para>posuere lacus in, accumsan nulla.</para><list>Cras dolor dui hendrerit eget eleifend eu 1</list><list>Cras dolor dui hendrerit eget eleifend eu 2</list><list>Cras dolor dui hendrerit eget eleifend eu 3</list></cd></paintings>';
$(function() {
  var out = $('#xmldata');
  var ul = null;
  $(xml).find('cd').children().each(function(_, node) {
    node = $(node);
    if (node.is('para')) {
      out.append($('<p>', {
        'text': node.text()
      }));
      ul = null;
    } else if (node.is('list')) {
      if (!ul) {
        ul = $('<ul>');
        out.append(ul);
      }
      ul.append($('<li>', {
        'text': node.text()
      }));
    } else {
      console.log('unknow node', node);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row" id="xmldata"></div>
1

You can do it by Domparser().

var text, parser, xmlDoc;
text = "<paintings><cd><para>posuere lacus in, accumsan nulla.</para><list>Cras dolor dui hendrerit eget eleifend eu</list><list>Cras dolor dui hendrerit eget eleifend eu</list><list>Cras dolor dui hendrerit eget eleifend eu</list></cd></paintings>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
xmlDoc.getElementsByTagName("para");//return node
xmlDoc.getElementsByTagName("list");//return list arrray
Negi Rox
  • 3,828
  • 1
  • 11
  • 18
0

You are not looping through all of your list items but are rather just taking the first one. Change your javascript to something like this:

function xmlParser(xml) {
  xml = $(xml).children();
  $(xml).children().each(function () {
    let para = '<div>' + $(this).find("para").text() + '</div>';
    let html = `<p>${para}</p>`;
    html += '<ul>'
    $(this).find("list").each(function () {
      html += `<li>${$(this).text()}</li>`;
    });
    html += '</ul>'
    $("#xmldata").append(html);
  });
}

Only change I made is that it will loop to every list item now so all of them are appended to the DOM.

etarhan
  • 4,138
  • 2
  • 15
  • 29