2

I need to get all the data contained in this file XML

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Tags>
      <Tag>
        <Nome>#SaintDenis</Nome>
        <Classe>21</Classe>
     </Tag>
    ....
    </Tags>

in order to create an array arr like this

    arr [[SaintDenis, 21]......]

how can i parse the structure of the xml file in javascript... thank you in advice!

3 Answers 3

1

You can do something like this with JQuery:

 $(document).ready(function()
      {
        $.get('myData.xml', function(d){
        $('body').append('<h1> Recommended Web Development Books </h1>');
        $('body').append('<dl />');

        $(d).find('book').each(function(){

            var $book = $(this); 
            var title = $book.attr("title");
            var description = $book.find('description').text();
            var imageurl = $book.attr('imageurl');

            var html = '<dt> <img class="bookImage" alt="" src="' + imageurl + '" /> </dt>';
            html += '<dd> <span class="loadingPic" alt="Loading" />';
            html += '<p class="title">' + title + '</p>';
            html += '<p> ' + description + '</p>' ;
            html += '</dd>';

            $('dl').append($(html));

            $('.loadingPic').fadeOut(1400);
        });
    });
});

See http://code.tutsplus.com/tutorials/quick-tip-use-jquery-to-retrieve-data-from-an-xml-file--net-390 for more information

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

Comments

1

You can use recursion.

function iterate (node) {
    for (var i = 0; i < node.childNodes.length; i++) {
      var child = node.childNodes[i];
      iterate(child);
      //gather info here
    }
}

Hope this helps!

Comments

0

This is your input, so:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Tags>
      <Tag>
        <Nome>#SaintDenis</Nome>
        <Classe>21</Classe>
     </Tag>
    ....
    </Tags>

The best thing is do some stuff like this:

function getValues ( XMLnode ) {
    var Values = [];
    var childs = XMLnode.childNodes;
    for (var i = 0; i < childs.length; i++) {
      var array_temp = [];
      if(childs[i].nodeName == 'Tag'){    // I think each tag have names and classe
         var childsTag = childs[i].childNodes;
         var array_temp_2 = [];
         for (var i = 0; i < childs.length; i++) {
             array_temp_2.push( childs[i].value ) ; // here you can use 'value'
                                                    // use your own tag('name' or whatever)
         }
         array_temp.push(array_temp_2);
      } 
       Values.push( array_temp );
    }
    return Values;
}

And thats all:

var valuesXML = [];
valuesXML = getValues( XMLnode ) ;

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.