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!
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
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!
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 ) ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.