drmong
drmong

Reputation: 13

Nested XML - Values in HTML-Tags

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

Upvotes: 0

Views: 169

Answers (1)

naota
naota

Reputation: 4718

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.

Upvotes: 1

Related Questions