jQuery XML 解析/遍历
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1022080/
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
jQuery XML Parsing/Traversing
提问by Yarin Miran
I have the following XML-
我有以下 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>
What I want to do is to loop each of the of a certain row.
我想要做的是循环某一行的每一个。
I tried to do this in order to get all of the attr ids but I also got the values ids.
我试图这样做是为了获得所有的 attr id,但我也得到了值 id。
function fillForm(id){
var theRow = $(theXmlDoc).find('row[id='+id+']').get()
$(theRow).find("attr").each(function(i)
{
alert($(this).find("id").text());
});
}
I also would like to note that main goal is loop each attr and afterwards to loop each value while I have the attr's id.
我还想指出,主要目标是循环每个 attr,然后在我拥有 attr 的 id 时循环每个值。
P.S if you think of an easier/simpler way to do so with some other library I'm open for suggestions.
PS,如果您想到使用其他一些库的更简单/更简单的方法,我愿意接受建议。
Thanks in advance,
提前致谢,
采纳答案by artlung
I'm not clear what you're trying to loop over, I think one of the tags in your question was garbled. You say: "What I want to do is to loop each of the of a certain row." and I think you had a tag in there.
我不清楚你想循环什么,我认为你问题中的一个标签是乱码。你说:“我想要做的是循环某一行的每一个。” 我认为你在那里有一个标签。
Anyway, here's some examples of extracting data from the parsed xml document using jQuery. The comments show what will be alerted.
无论如何,这里有一些使用 jQuery 从解析的 xml 文档中提取数据的示例。评论显示将收到警报的内容。
I think part of the problem is you have the id values as siblings rather than children to the attributes. It seems like a more coherent structure might be:
我认为问题的一部分是您将 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>
But if you don't have control over the xml, please ignore that suggestion. :-)
但是,如果您无法控制 xml,请忽略该建议。:-)
Okay, here are some samples of traversal to get various pieces of data:
好的,这里有一些遍历样本来获取各种数据:
First let's just get "Item1"
首先让我们得到“Item1”
<script type="text/javascript">
// Item1
$.get('sample.xml', null, function (data, textStatus) {
alert( $(data).find('rows row[id=5] cell').text());
}, 'xml');
</script>
Now we'll get the 1 and the 2:
现在我们将得到 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>
And lastly, let's pull out the main attr ids and tie them into the values:
最后,让我们拉出主要的属性并将它们绑定到值中:
<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>
回答by Artem Barger
This should work for you
这应该适合你
function fillForm(id){
var row = $(theXmlDoc).find('row#+'+ id);
var ids = row.find( "attr > id");
ids.each(function(i)
{
alert($(this).text());
});
}
PS. You can use xpatheither:
附注。您可以使用xpath:
var ids = document.evaluate( '//rows/row[@id='+id + ']/attr/id/text( )', theXmlDoc, null, XPathResult.ANY_TYPE, null );