我在PhoneGap中使用xui js,我想通过xui js读取xml。我写了一些代码,我正在为每个元素运行根元素successfully.And循环,我还获得了能够显示textContent.But的元素我无法在element.How中获得更多标记内的元素我可以获得如下that.My代码:
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 I want to get further elements by its tag name
});
}发布于 2013-04-18 16:15:26
您可以轻松地使用Here中的javascript for that.Get reference
或者用以下代码替换您的成功代码:-
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));
}https://stackoverflow.com/questions/16077392
复制相似问题