Yar*_*ran 16 xml jquery parsing traversal xml-parsing
我有以下XML-
<rows>
<row id="5">
<cell>Item1</cell>
<attrs>
<attr>
<id>1</id>
<type>CheckBox</type>
<values>
<value>
<id>10</id>
</value>
<value>
<id>11</id>
</value>
</values>
</attr>
<attr>
<id>2</id>
<type>CheckBox</type>
<values>
<value>
<id>20</id>
</value>
<value>
<id>21</id>
</value>
</values>
</attr>
</attrs>
</row>
</rows>
Run Code Online (Sandbox Code Playgroud)
我想要做的是循环某一行的每一行.
我试图这样做以获得所有的attr id但我也得到了值ids.
function fillForm(id){
var theRow = $(theXmlDoc).find('row[id='+id+']').get()
$(theRow).find("attr").each(function(i)
{
alert($(this).find("id").text());
});
}
Run Code Online (Sandbox Code Playgroud)
我还要注意,主要目标是循环每个attr,然后循环每个值,同时我有attr的id.
PS如果你想到一个更简单/更简单的方法与其他一些图书馆这样做,我愿意接受建议.
提前致谢,
art*_*ung 14
我不清楚你想要循环什么,我认为你问题中的一个标签是乱码.你说:"我想要做的是循环每一行." 而且我觉得你有一个标签.
无论如何,这里有一些使用jQuery从解析的xml文档中提取数据的例子.评论显示将被提醒的内容.
我认为问题的一部分是你有id值作为兄弟姐妹而不是孩子的属性.似乎一个更连贯的结构可能是:
<rows>
<row id="5">
<cell>Item1</cell>
<attrs>
<attr id="1">
<type>CheckBox</type>
<values>
<value>
<id>10</id>
</value>
<value>
<id>11</id>
</value>
</values>
</attr>
<attr id="2">
<type>CheckBox</type>
<values>
<value>
<id>20</id>
</value>
<value>
<id>21</id>
</value>
</values>
</attr>
</attrs>
</row>
</rows>
Run Code Online (Sandbox Code Playgroud)
但如果您无法控制xml,请忽略该建议.:-)
好的,这里有一些遍历的样本来获取各种数据:
首先让我们得到"Item1"
<script type="text/javascript">
// Item1
$.get('sample.xml', null, function (data, textStatus) {
alert( $(data).find('rows row[id=5] cell').text());
}, 'xml');
</script>
Run Code Online (Sandbox Code Playgroud)
现在我们将获得1和2:
<script type="text/javascript">
// 1
// 2
$.get('sample.xml', null, function (data, textStatus) {
$(data).find('rows row[id=5] attrs attr > id').each( function(){
alert($(this).text()); // 1, 2
});
}, 'xml');
</script>
Run Code Online (Sandbox Code Playgroud)
最后,让我们拉出主要的attr id并将它们绑定到值中:
<script type="text/javascript">
// rows row[id=5] attrs attr > id 1 has inner ids of 10,11
// rows row[id=5] attrs attr > id 2 has inner ids of 20,21
$.get('sample.xml', null, function (data, textStatus) {
var out = ''
$(data).find('rows row[id=5] attrs attr > id').each( function(){
out += 'rows row[id=5] attrs attr > id ' + $(this).text();
var innerIds = [];
$(this).siblings('values').find('value id').each(function(){
innerIds.push($(this).text())
});
out += ' has inner Ids of ' + innerIds.join(',') + '\n';
});
alert(out);
}, 'xml');
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34597 次 |
| 最近记录: |