find xml element by attribute

别来无恙 提交于 2019-12-13 15:30:36

问题


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();

the 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>

回答1:


With jQuery, you could do:

var result = $(xml).find("show[id=2] > gallery").text();

As in:

$.ajax({
    url:'/path/to/file.xml',
    success:function(xml) {
        var result = $(xml).find("show[id=2] > gallery").text();
        alert(result);
    }
});

EDIT: Added the > to the selector. Not required, but a little better.



来源:https://stackoverflow.com/questions/3024276/find-xml-element-by-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!