javascript - How to read xml in xui js -
i using xui js in phonegap , want read xml through xui js.i have written code , getting root element successfully.and running forloop each element getting element able display textcontent.but not able further elements inside tags in element.how can that.my code follows:
xui(window).on('load', function(){ var url="http://www.w3schools.com/dom/books.xml"; fireurl(url,winner); }); function fireurl(url,success) { var option={ async:true, callback:function(){ success(this.responsetext); } } x$().xhr(url,option); } function winner(data) { domparser = new domparser(); var xmldoc=domparser.parsefromstring(data,"text/xml"); x$(xmldoc).find("book").each(function(element,index){ alert(element.textcontent); //here want further elements tag name }); }
you can use javascript that.get reference here
or replace success code following code:-
function winner(data) { domparser = new domparser(); var xmldoc=domparser.parsefromstring(data,"text/xml"); var _bookstore=[]; x$(xmldoc).find("book").each(function(element,index){ var _book={}; _book.author=[]; _book.title=element.getelementsbytagname("title")[0].textcontent; for(j=0;j<element.getelementsbytagname("author").length;j++){ _book.author.push(element.getelementsbytagname("author")[j].textcontent); } _book.year=element.getelementsbytagname("year")[0].textcontent; _book.price=element.getelementsbytagname("price")[0].textcontent; _bookstore.push(_book); }); alert("hello"+json.stringify(_bookstore)); }
Comments
Post a Comment