I am trying to parse the XML response from a GET request made from jQuery. The problem seems to be the type of the XML element that I am interested in.
我试图解析来自jQuery的GET请求的XML响应。问题似乎是我感兴趣的XML元素的类型。
Please let me know how I can parse this.
请让我知道如何解析这个问题。
<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://purl.org/atom/app#"
xmlns:yt="http://gdata.youtube.com/schemas/2007" xmlns:gd="http://schemas.google.com/g/2005"
xmlns:media="http://search.yahoo.com/mrss/">
<id>http://gdata.youtube.com/feeds/api/videos/VVoO9cdk5Eo</id>
<published>2015-04-03T18:30:00.000Z</published>
<updated>2015-04-06T02:11:57.000Z</updated>
<gd:rating average="4.9371624" max="5" min="1" numRaters="2037"
rel="http://schemas.google.com/g/2005#overall" />
<yt:statistics favoriteCount="0" viewCount="148847" />
With my code, I can successfully retrieve something like 'published'. But I need to parse 'gd:rating'
使用我的代码,我可以成功检索“已发布”之类的内容。但我需要解析'gd:rating'
$(document).ready(function(){
$("#mybutton").click(function(){
$.ajax({
type: "GET",
dataType: "xml",
url: "https://gdata.youtube.com/feeds/api/videos/VVoO9cdk5Eo?v=1",
success: function(response){
console.log("Success");
var rating = $(response).find('published').first().text();
alert("#" + rating);
}
});
});
});
1 个解决方案
#1
0
jQuery's XML parsing capabilities are not so great, especially when it comes to namespaces.
jQuery的XML解析功能并不是那么好,尤其是在命名空间方面。
You can work around this easily by using standard DOM methods.
您可以使用标准DOM方法轻松解决此问题。
$(function(){
$("#mybutton").click(function () {
var videoInfo = "https://gdata.youtube.com/feeds/api/videos/VVoO9cdk5Eo?v=1";
$.get(videoInfo).done(function (xml) {
var rating = xml.getElementsByTagName("rating")[0];
if (rating) {
alert("Average rating: " + rating.getAttribute("average"));
} else {
alert("Sorry, no rating.");
}
});
});
});
#1
0
jQuery's XML parsing capabilities are not so great, especially when it comes to namespaces.
jQuery的XML解析功能并不是那么好,尤其是在命名空间方面。
You can work around this easily by using standard DOM methods.
您可以使用标准DOM方法轻松解决此问题。
$(function(){
$("#mybutton").click(function () {
var videoInfo = "https://gdata.youtube.com/feeds/api/videos/VVoO9cdk5Eo?v=1";
$.get(videoInfo).done(function (xml) {
var rating = xml.getElementsByTagName("rating")[0];
if (rating) {
alert("Average rating: " + rating.getAttribute("average"));
} else {
alert("Sorry, no rating.");
}
});
});
});