如何使用jquery读取通用xml
我有一个通用xml,如下所示如何使用jquery读取通用xml,jquery,xml,xml-parsing,Jquery,Xml,Xml Parsing,我有一个通用xml,如下所示 <Results> <Columns> <Column>Center ID</Column> <Column>Center Name</Column> /Columns> <Result> <Data>32452368</Data> <Data>Center1</Data> &l
<Results>
<Columns>
<Column>Center ID</Column>
<Column>Center Name</Column>
/Columns>
<Result>
<Data>32452368</Data>
<Data>Center1</Data>
</Result>
<Result>
<Data>32452368</Data>
<Data> Center2</Data>
</Result>
</Results>
但我需要知道一些方法,按顺序读取它们,就像第一个数据总是中心ID一样。
任何建议
谢谢,您位于右侧,
每个
方法迭代jQuery对象,按顺序为每个匹配的元素执行函数,但是查找
方法中选择器的语法不正确,请尝试以下操作:
$(xml).find('Results > Result').each(function(){
// do something here
})
像这样的怎么样:
var columns = [];
// Go through the first Result tag and record the order of the columns:
$(xml).find('Results > Result:first').each(function() {
$(this).children('Data').each(function() {
columns.push( $(this).text() );
});
});
var results = [];
$(xml).find('Results > Result:not(:first)').each(function() {
// Result will map column names to values:
var result = {};
$(this).children('Data').each(function(i) {
result[ columns[i++] ] = $(this).text();
});
results.push(result);
});
// Now results is an array of objects:
// [
// { 'Center ID': 32452368', 'Center Name': 'Center1' },
// { 'Center ID': 32452368', 'Center Name': 'Center2' }
// ]
唯一让您走错方向的是在选择器中使用了
/
,这不是有效的CSS/jQuery选择器<代码>>用于指示父/子关系<代码>结果>结果是关键。谢谢您的建议。。我正在实施过程中,完成后将更新状态。谢谢有没有任何方法可以获得数据结果:第一,结果:第二,结果:第三。。类似于使用jqueryYes:eq()
是该选项的选择器。
var columns = [];
// Go through the first Result tag and record the order of the columns:
$(xml).find('Results > Result:first').each(function() {
$(this).children('Data').each(function() {
columns.push( $(this).text() );
});
});
var results = [];
$(xml).find('Results > Result:not(:first)').each(function() {
// Result will map column names to values:
var result = {};
$(this).children('Data').each(function(i) {
result[ columns[i++] ] = $(this).text();
});
results.push(result);
});
// Now results is an array of objects:
// [
// { 'Center ID': 32452368', 'Center Name': 'Center1' },
// { 'Center ID': 32452368', 'Center Name': 'Center2' }
// ]