jquery 解析 xml 多个标签
jquery parse xml for multiple tags
我想从一个 XML 文档中获取多个标签。我查看了 this question ,它基本上获取了一个标签的子元素,而我想要所有标签。使用相同的示例 XML(复制如下):
var data =
"<root>
<instructions>Some ins text.</instructions>
<options>
<option>1.png</option>
<option>2.png</option>
<option>3.png</option>
<option>4.png</option>
</options>
<noOfOptions>4</noOfOptions>
</root>";
如何获取 "instructions"、"options" 和 "noOfOptions" 的值?
我可以像这样重复代码三遍:
xml = $.parseXML(data),
$data = $( xml ),
$options = $data.find("option"); // get all option nodes
xml = $.parseXML(data),
$data = $( xml ),
$instructions = $xml.find("instructions"); // get instructions
xml = $.parseXML(data),
$data = $( xml ),
$noOfOptions = $xml.find("noOfOptions"); // get noOfOptions
但这似乎不对。有没有正确(且更有效)的方法来做到这一点?我觉得.each函数有点关系,但是不太懂怎么用
如果你的结构是固定的,你可以试试这个:
var data =
"<root>"+
"<instructions>Some ins text.</instructions>"+
"<options>"+
" <option>1.png</option>"+
"<option>2.png</option>"+
" <option>3.png</option>"+
" <option>4.png</option>"+
" </options>"+
" <noOfOptions>4</noOfOptions>"+
"<instructions>Some ins text1.</instructions>"+
"<options>"+
" <option>1.png</option>"+
"<option>2.png</option>"+
" <option>3.png</option>"+
" <option>4.png</option>"+
" </options>"+
" <noOfOptions>4</noOfOptions>"+
"</root>";
xml = $.parseXML(data);
var arr=[];
var arr=[];
$("instructions", xml).each(function (index,val) {
arr[index]=[];
arr[index]['instructions']=$(this).text();
arr[index]['option']=[];
$("option",$(val).next()).each(function(){
arr[index]['option'].push($(this).text());
});
arr[index]['noOfOptions']=[];
arr[index]['noOfOptions']=$($(val).next()).next().text();
});
console.log(arr);
控制台输出:
[Array[0], Array[0]]0: Array[0]instructions: "Some ins text."length:
0noOfOptions: "4"option: Array[4]proto: Array[0]1: Array[0]length:
2__proto__: Array[0]
如果您需要整个数据为 json(可以在 jquery array/object 中转换),您可以使用:xml2json
var data =
"<root>\
<instructions>Some ins text.</instructions>\
<options>\
<option>1.png</option>\
<option>2.png</option>\
<option>3.png</option>\
<option>4.png</option>\
</options>\
<noOfOptions>4</noOfOptions>\
</root>";
var xml = $.parseXML(data);
var $data = $( xml );
var instructions = $data.find("instructions").text();
var options = [];
$data.find("option").each(function(){
options.push($(this).text());
});
console.log("instructions: " + instructions);
console.log("options: ", options);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我想从一个 XML 文档中获取多个标签。我查看了 this question ,它基本上获取了一个标签的子元素,而我想要所有标签。使用相同的示例 XML(复制如下):
var data =
"<root>
<instructions>Some ins text.</instructions>
<options>
<option>1.png</option>
<option>2.png</option>
<option>3.png</option>
<option>4.png</option>
</options>
<noOfOptions>4</noOfOptions>
</root>";
如何获取 "instructions"、"options" 和 "noOfOptions" 的值?
我可以像这样重复代码三遍:
xml = $.parseXML(data),
$data = $( xml ),
$options = $data.find("option"); // get all option nodes
xml = $.parseXML(data),
$data = $( xml ),
$instructions = $xml.find("instructions"); // get instructions
xml = $.parseXML(data),
$data = $( xml ),
$noOfOptions = $xml.find("noOfOptions"); // get noOfOptions
但这似乎不对。有没有正确(且更有效)的方法来做到这一点?我觉得.each函数有点关系,但是不太懂怎么用
如果你的结构是固定的,你可以试试这个:
var data =
"<root>"+
"<instructions>Some ins text.</instructions>"+
"<options>"+
" <option>1.png</option>"+
"<option>2.png</option>"+
" <option>3.png</option>"+
" <option>4.png</option>"+
" </options>"+
" <noOfOptions>4</noOfOptions>"+
"<instructions>Some ins text1.</instructions>"+
"<options>"+
" <option>1.png</option>"+
"<option>2.png</option>"+
" <option>3.png</option>"+
" <option>4.png</option>"+
" </options>"+
" <noOfOptions>4</noOfOptions>"+
"</root>";
xml = $.parseXML(data);
var arr=[];
var arr=[];
$("instructions", xml).each(function (index,val) {
arr[index]=[];
arr[index]['instructions']=$(this).text();
arr[index]['option']=[];
$("option",$(val).next()).each(function(){
arr[index]['option'].push($(this).text());
});
arr[index]['noOfOptions']=[];
arr[index]['noOfOptions']=$($(val).next()).next().text();
});
console.log(arr);
控制台输出:
[Array[0], Array[0]]0: Array[0]instructions: "Some ins text."length: 0noOfOptions: "4"option: Array[4]proto: Array[0]1: Array[0]length: 2__proto__: Array[0]
如果您需要整个数据为 json(可以在 jquery array/object 中转换),您可以使用:xml2json
var data =
"<root>\
<instructions>Some ins text.</instructions>\
<options>\
<option>1.png</option>\
<option>2.png</option>\
<option>3.png</option>\
<option>4.png</option>\
</options>\
<noOfOptions>4</noOfOptions>\
</root>";
var xml = $.parseXML(data);
var $data = $( xml );
var instructions = $data.find("instructions").text();
var options = [];
$data.find("option").each(function(){
options.push($(this).text());
});
console.log("instructions: " + instructions);
console.log("options: ", options);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>