1

I am trying to read XML elements without using elements name i.e. "Books" from the following example. for e.g.

  <Books>
           <book>
              <title>C++</title>
              <author>A</author>
           </book>
           <book>
              <title>XML</title>
              <author>X</author>
           </book>
    </Books>

In other words How to dynamically read "Books" element and assign children element to array using jquery in HTML. Any help would be appreciated.

7
  • have you tried something? Commented Feb 18, 2013 at 17:39
  • have you looked at: api.jquery.com/jQuery.parseXML ? Commented Feb 18, 2013 at 17:40
  • Hello Lakhae, try this killertilapia.blogspot.in/2011/03/… Commented Feb 18, 2013 at 17:41
  • Thank you Naveen. I can read the xml file by using $(this).find('text').text(); Now what I am trying to do is without giving 'text' how can I read all the sub elements of <Formdata> and assign them to array. Commented Feb 18, 2013 at 17:50
  • You can't. You can't select something from nothing? You can use children(), closest(), first(), last(), next() and a hundred other methods without using the tagname, but you can't get all tags with a certain name unless you select them by name, or if they are all children of the same element etc. Commented Feb 18, 2013 at 17:52

2 Answers 2

1

I wrote simple HTML for you. Demo at http://jsfiddle.net/UC2dM/185/

$.ajax({
    url:'/echo/xml/',
    data: {xml:'<Books><book><title>C++</title><author>A</author> </book><book><title>XML</title><author>X</author></book></Books>'},
    dataType: 'xml',
    type:'post',
    success: function(data){
        var xml = $(data);
        $('#container').append( CategoryToUl(xml.children()) );
    }
});

function CategoryToUl(xml){
    var categories = xml.children('book');
    if (categories.length > 0)
    {
        var ul = $('<ul/>');
        categories.each(function(){
            var $this = $(this);
            var li = $('<li/>');
            var a = $('<a/>',{
                text: $this.children('title').text()
            });
            li.append(a);
            li.append( CategoryToUl( $this ) );
            ul.append(li);
        });
        return ul;
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but that is not what I am looking for.
0

Thank you all. I have found my solution

var xml = "<root><stuff></stuff><stuff><stuffchild></stuffchild></stuff></root>";

function alertit(jqueryObject) {
    if (jqueryObject.length === 0) return;

    jqueryObject.each(function() {
        alert(this.nodeName.toLowerCase());
    });

    alertit(jqueryObject.children());
}

alertit($(xml));

http://jsfiddle.net/TBwm8/3/

Thanks.

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.