0

I have an nested XML-File like this:

<?xml version="1.0" encoding="utf-8" ?>
<xml>
  <section name="Highlights">
    <sub name="Highlights" open="Selected Top Events">
      <date name="Offroad;Experience" region="Europe" location="Austria / Lech">
        <detail from="01.01.2014" to="09.03.2014" thumb="thumb" pic="pic" url="url" text="text"/>
      </date>
    </sub>
  </section>
</xml>

What i want is, i need the values from the Attributes in different HTML-Elements. But how? The Problem is that search for the Element section and then i show the value of the name attribute, but i have also a name attribute in date. Here is my jQuery:

$.ajax({
    url: 'data.xml',
    dataType: 'xml',
    success: function(data) {
      //console.log(data);
      $(data).find('xml section').each(function() {
            var section = $(this).attr('name');
            var name = $(this).attr('name');
            var user = $(this).find('user name').text();
            var sub = $(this).attr('open');


            $('.output').append(
                $('<div />', {
                    text: section 
                })
            );
      });
    },
    error: function() {
      $('.output').text('XML fail');
    }
});
4
  • jquery is not fully compatible with xml. Commented Jun 13, 2014 at 11:25
  • can you tell me some details? Commented Jun 13, 2014 at 11:27
  • I can't even tell what the problem is... Commented Jun 13, 2014 at 11:28
  • the problem is that I do not know how I can write different values ​​in different divs Commented Jun 13, 2014 at 11:31

1 Answer 1

1

I guess you might want to use $.parseXML() like this:

var data = '<section name="Highlights"><sub name="Highlights" open="Selected Top Events">
     ...</sub></section>',
xmlDoc = $.parseXML( data ),
$xml = $(xmlDoc),
name = $xml.find('sub').attr('name');

alert(name); // shows 'Highlights'

DEMO:http://jsfiddle.net/S7nac/

The Document of $.parseXML() is here :
http://api.jquery.com/jquery.parsexml/

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.