按属性查找xml元素 [英] find xml element by attribute
本文介绍了按属性查找xml元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用JQuery或Javascript如何从下面的xml返回'Mary Boone',首先使用show'id'属性'2'?
Using JQuery or Javascript how would I return 'Mary Boone' from the xml below starting out with the show 'id' attribute of '2'?
我正在考虑的事情 -
I'm thinking something along the lines of -
var result = xml.getElementByAttribute("2").gallery.text();
XML:
<shows>
<show id="1">
<artist>Andreas Gursky</artist>
<gallery>Matthew Marks</gallery>
<medium>photography</medium>
</show>
<show id="2">
<artist>Eric Fischl</artist>
<gallery>Mary Boone</gallery>
<medium>painting</medium>
</show>
</shows>
推荐答案
使用jQuery,你可以这样做:
With jQuery, you could do:
var result = $(xml).find("show[id=2] > gallery").text();
如下:
$.ajax({
url:'/path/to/file.xml',
success:function(xml) {
var result = $(xml).find("show[id=2] > gallery").text();
alert(result);
}
});
编辑:添加>
到选择器。不是必需的,但要好一点。
Added the >
to the selector. Not required, but a little better.
这篇关于按属性查找xml元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文