开发者

find xml element by attribute

开发者 https://www.devze.com 2023-01-02 23:32 出处:网络
Using JQuery or开发者_高级运维 Javascript how would I return \'Mary Boone\' from the xml below starting out with the show \'id\' attribute of \'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();

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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号