使用 jQuery 将 XML 转换为 javascript 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6542187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML to javascript array with jQuery
提问by Joshua Welker
I am new to XML and AJAX and am only a newcomer to Javascript and jQuery. Among other job duties I design our website. A deadline is very near, and the only way I can think of to do this project well is with AJAX. I have a document full of XML objects such as this one repeating:
我是 XML 和 AJAX 的新手,我只是 Javascript 和 jQuery 的新手。在其他工作职责中,我设计了我们的网站。截止日期很近,我能想到的做好这个项目的唯一方法就是使用 AJAX。我有一个充满 XML 对象的文档,例如这个重复的:
<item>
<subject></subject>
<date></date>
<thumb></thumb>
</item>
I want to create an array of all elements and their child elements. I've been reading jQuery tutorials on AJAX for hours and don't even know where to start because they all assume a certain level of javascript proficiency. If someone could show me the easiest way to loop through all elements and put their children into an array, I'd appreciate it tons.
我想创建一个包含所有元素及其子元素的数组。我一直在阅读有关 AJAX 的 jQuery 教程好几个小时,甚至不知道从哪里开始,因为它们都假定了一定程度的 javascript 熟练程度。如果有人可以向我展示遍历所有元素并将他们的孩子放入数组的最简单方法,我将不胜感激。
回答by madfriend
Using jQuery, $.ajax()
your XML file, and on success pass retrieved data with each
, like:
使用 jQuery、$.ajax()
您的 XML 文件,并在成功时使用 传递检索到的数据each
,例如:
var tmpSubject, tmpDate, tmpThumb;
$.ajax({
url: '/your_file.xml',
type: 'GET',
dataType: 'xml',
success: function(returnedXMLResponse){
$('item', returnedXMLResponse).each(function(){
tmpSubject = $('subject', this).text();
tmpDate = $('date', this).text();
tmpThumb = $('thumb', this).text();
//Here you can do anything you want with those temporary
//variables, e.g. put them in some place in your html document
//or store them in an associative array
})
}
});
回答by Troublesum
I wrote this.. pretty simple way to take a welformed XML response/string and parse it with jquery and then convert to array.
我写了这个..非常简单的方法来获取格式良好的 XML 响应/字符串并用 jquery 解析它,然后转换为数组。
var response = '<?xml version="1.0" encoding="UTF-8"?><root><node1>something</node1></root>'
var xmlDoc = $.parseXML( response );
var myArray = getXMLToArray(xmlDoc);
alert(myArray['root']['node1']);
//Pop up window displaying the text "something"
function getXMLToArray(xmlDoc){
var thisArray = new Array();
//Check XML doc
if($(xmlDoc).children().length > 0){
//Foreach Node found
$(xmlDoc).children().each(function(){
if($(xmlDoc).find(this.nodeName).children().length > 0){
//If it has children recursively get the inner array
var NextNode = $(xmlDoc).find(this.nodeName);
thisArray[this.nodeName] = getXMLToArray(NextNode);
} else {
//If not then store the next value to the current array
thisArray[this.nodeName] = $(xmlDoc).find(this.nodeName).text();
}
});
}
return thisArray;
}
Hope this helps!!
希望这可以帮助!!
回答by Bassim Huis
I added to your script Troublesum
我在你的脚本中添加了 Troublesum
function getXMLToArray(xmlDoc){
var thisArray = new Array();
//Check XML doc
if($(xmlDoc).children().length > 0){
//Foreach Node found
$(xmlDoc).children().each(function(){
if($(xmlDoc).find(this.nodeName).children().length > 0){
//If it has children recursively get the inner array
var NextNode = $(xmlDoc).find(this.nodeName);
thisArray[this.nodeName] = getXMLToArray(NextNode);
} else {
//If not then store the next value to the current array
thisArray[this.nodeName] = [];
$(xmlDoc).children(this.nodeName).each(function(){
thisArray[this.nodeName].push($(this).text());
});
}
});
}
return thisArray;
}
It now also supports many children with same name in XML. f.e
它现在还支持在 XML 中具有相同名称的许多子项。铁
XML being
XML 被
<countries>
<NL>
<borders>
<country>Germany</country>
<country>Belgium</country>
countries.NL.borders[1] will give Germany.
country.NL.borders[1] 将给德国。