如何使用 Javascript 或 jQuery 解析 xml webservice 响应
我想使用以下 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:
刚刚检查了网络服务,我发现并非每个
艺术家
都指定了性别
标签。在这种情况下,您可以使用以下内容:请参阅此处的 JSFiddle 演示。
Update:
Just checked the webservice and I saw that not every
artist
has agender
tag specified. In this case you can use the following:See JSFiddle Demo here.