-1

I have a problem with loading my XML properly. If I have more than one chat-tag element in my XMl, it will not load anything from it. But then I have just one, it works fine. Any ideas why?

Here is my code:

enter image description here

My XML:

enter image description here

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
  • I think you should parse xml at first and then start loop. See this answer how to parse XML http://stackoverflow.com/a/7228219/3917754 – demo Apr 17 '15 at 09:06
  • 1
    Please post _actual_ code inside your question, and not just a screenshot _of_ code. – CBroe Apr 17 '15 at 10:12

1 Answers1

3

The root element in your XML is not well-formed. Have you tried to validate it?

I edited your XML file to this:

<?xml version="1.0" standalone="yes" ?>
<chat>
  <message>
    <username>Mon</username>
    <msg>Hi son!</msg>
  </message>
  <message>
    <username>Lund</username>
    <msg>Hi mom!</msg>
  </message>
</chat>

And your JavaScript function to this:

function loadXML() {
  var lix = '<li class="box pre-post">';
  $.ajax({
    type: 'GET',
    url: 'posts.xml',
    datatype: 'xml',
    success: function(data) {
      $('.posts').children().remove();
      $(data).find('message').each(function() {
        var info = lix + $(this).find('username').text() + $(this).find('msg').text() + '</li>';
        $('.posts').prepend(info);
      });
    }
  });
}

And it seems to work

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
guille11
  • 71
  • 2
  • 5