如何使用 Javascript 或 jQuery 解析 xml webservice 响应

发布于 2025-01-07 04:44:54 字数 1170 浏览 2 评论 0原文

我想使用以下 xml web 服务。 www.musicbrainz.org/ws/2/artist/? query=artist:michael jackson

其格式如下:

<metadata><artist-list offset="0" count="3418"><artist ext:score="100" type="Person" id="f27ec8db-af05-4f36-916e-3d57f91ecf5e"><name>Michael Jackson</name><sort-name>Jackson, Michael</sort-name><gender>male</gender><country>US</country>

我只想解析这个 xml &从中获取性别。 我使用以下代码来解析 xml 。 在这里我得到了艺术家的 ext 属性但不起作用。

    $.ajax({
        type: 'GET',
        url: 'http://www.musicbrainz.org/ws/2/artist/?query=artist:michael jackson',
        dataType: 'xml',
        success: function(xml){
           // console.log(xml);
            $(xml).find('artist-list').each(function(){
            $(this).find('artist').each(function(){
                            var ext = $(this).attr('ext');
                            alert(ext);
                    });
            });
    }

});

任何人都可以建议我使用 Javascript 或 jQuery 解析 xml 的示例。

I want to use following xml webservice.
www.musicbrainz.org/ws/2/artist/?query=artist:michael jackson

which format is like below:

<metadata><artist-list offset="0" count="3418"><artist ext:score="100" type="Person" id="f27ec8db-af05-4f36-916e-3d57f91ecf5e"><name>Michael Jackson</name><sort-name>Jackson, Michael</sort-name><gender>male</gender><country>US</country>

I just want to parse this xml & get the gender from it .
I used following code to parse xml .
Here i get ext attribute of the artist but not working .

    $.ajax({
        type: 'GET',
        url: 'http://www.musicbrainz.org/ws/2/artist/?query=artist:michael jackson',
        dataType: 'xml',
        success: function(xml){
           // console.log(xml);
            $(xml).find('artist-list').each(function(){
            $(this).find('artist').each(function(){
                            var ext = $(this).attr('ext');
                            alert(ext);
                    });
            });
    }

});

Anybody can suggest me the example to parse xml using Javascript or jQuery.

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

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

发布评论

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

评论(1

柠檬心 2025-01-14 04:44:54
$.ajax({ 
    type: 'GET', 
    url: 'http://www.musicbrainz.org/ws/2/artist/?query=artist:michael jackson',
    dataType: 'xml', 
    success: function(xml){ 
       $("artist", xml).each(function(){
           console.log($("gender", this).text());
       });
    }
});

更新:

刚刚检查了网络服务,我发现并非每个艺术家都指定了性别标签。在这种情况下,您可以使用以下内容:

    $("artist", xml).each(function(){
         var gender = $("gender", this);
         if(gender.length>0)
             console.log($(gender).text());
    });

请参阅此处的 JSFiddle 演示。

$.ajax({ 
    type: 'GET', 
    url: 'http://www.musicbrainz.org/ws/2/artist/?query=artist:michael jackson',
    dataType: 'xml', 
    success: function(xml){ 
       $("artist", xml).each(function(){
           console.log($("gender", this).text());
       });
    }
});

Update:

Just checked the webservice and I saw that not every artist has a gender tag specified. In this case you can use the following:

    $("artist", xml).each(function(){
         var gender = $("gender", this);
         if(gender.length>0)
             console.log($(gender).text());
    });

See JSFiddle Demo here.

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