按属性查找 xml 元素

发布于 2024-09-05 06:41:32 字数 630 浏览 2 评论 0原文

使用 JQuery 或 Javascript,我如何从下面的 xml 返回“Mary Boone”,并以“2”的显示“id”属性开始?

我正在思考一些类似于

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卷耳 2024-09-12 06:41:32

使用 jQuery,您可以执行

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

编辑:> 添加到选择器。不是必须的,但是稍微好一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文