Jquery 解析 XML 失败
我正在 AIR 应用程序中解析 youtube 视频源。 使用jquery来解析它。
$(feed).find("entry").each(function()
{
var tit = $(this).find('title');
alert(tit.text());
}
似乎在解析category
之后(类别标签以不同的方式关闭),它无法解析文档的其余部分。上面的代码成功提醒文本,直到updated
标签。什么可以成为解决方案。
<entry gd:etag='W/"C0AMRn47eCp7ImA9WxRQGUw."'>
<id>tag:youtube,2008:video:ZTUVgYoeN_b</id>
<published>2008-07-05T19:56:35.000-07:00</published>
<updated>2008-07-18T07:21:59.000-07:00</updated>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://gdata.youtube.com/schemas/2007#video'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat'
term='Shopping'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat'
term='parkas'/>
<category scheme='http://gdata.youtube.com/schemas/2007/categories.cat'
term='People' label='People'/>
<title>Shopping for Coats</title>
<content type='application/x-shockwave-flash'
src='http://www.youtube.com/v/ZTUVgYoeN_b?f=gdata_standard...'/>
<link rel='alternate' type='text/html'
href='http://www.youtube.com/watch?v=ZTUVgYoeN_b'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.responses'
type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/responses?v=2'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.ratings'
type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/ratings?v=2'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.complaints'
type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/complaints?v=2'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.related'
type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/related?v=2'/>
<link rel='http://gdata.youtube.com/schemas/2007#mobile'
type='text/html' href='http://m.youtube.com/details?v=ZTUVgYoeN_b'/>
<link rel='self' type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/standardfeeds/top_rated/v/ZTUVgYoeN_b?v=2'/>
<author>
<name>andyland74</name>
<uri>http://gdata.youtube.com/feeds/api/users/andyland74</uri>
</author>
<media:group>
<media:title type='plain'>Shopping for Coats</media:title>
<media:description type='plain'>
What could make for more exciting video?
</media:description>
<media:keywords>Shopping, parkas</media:keywords>
<yt:duration seconds='79'/>
<yt:videoid>ZTUVgYoeN_b</yt:videoid>
<media:player url='http://www.youtube.com/watch?v=ZTUVgYoeN_b'/>
<media:credit role='uploader' scheme='urn:youtube'>andyland74</media:credit>
<media:category label='People'
scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>People
</media:category>
<media:content
url='http://www.youtube.com/v/ZTUVgYoeN_b?f=gdata_standard...'
type='application/x-shockwave-flash' medium='video'
isDefault='true' expression='full' duration='215' yt:format='5'/>
<media:content
url='rtsp://rtsp2.youtube.com/ChoLENy73bIAEQ1kgGDA==/0/0/0/video.3gp'
type='video/3gpp' medium='video'
expression='full' duration='215' yt:format='1'/>
<media:content
url='rtsp://rtsp2.youtube.com/ChoLENy73bIDRQ1kgGDA==/0/0/0/video.3gp'
type='video/3gpp' medium='video'
expression='full' duration='215' yt:format='6'/>
<media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/2.jpg'
height='97' width='130' time='00:00:03.500'/>
<media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/1.jpg'
height='97' width='130' time='00:00:01.750'/>
<media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/3.jpg'
height='97' width='130' time='00:00:05.250'/>
<media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/0.jpg'
height='240' width='320' time='00:00:03.500'/>
</media:group>
<yt:statistics viewCount='383290' favoriteCount='7022'/>
<gd:rating min='1' max='5' numRaters='14763' average='4.93'/>
<georss:where>
<gml:Point>
<gml:pos>21.37124437061831 -157.87353515625</gml:pos>
</gml:Point>
</georss:where>
<gd:comments>
<gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/comments'
countHint='9416'/>
</gd:comments>
</entry>
I am parsing youtube video feed in AIR app .
Using jquery to parse it.
$(feed).find("entry").each(function()
{
var tit = $(this).find('title');
alert(tit.text());
}
It seems like after parsing category
(category tag is closed a different way) it fails to parse rest of the doc.The above code successfully alerts text till updated
tag.What can be the solution.
<entry gd:etag='W/"C0AMRn47eCp7ImA9WxRQGUw."'>
<id>tag:youtube,2008:video:ZTUVgYoeN_b</id>
<published>2008-07-05T19:56:35.000-07:00</published>
<updated>2008-07-18T07:21:59.000-07:00</updated>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://gdata.youtube.com/schemas/2007#video'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat'
term='Shopping'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat'
term='parkas'/>
<category scheme='http://gdata.youtube.com/schemas/2007/categories.cat'
term='People' label='People'/>
<title>Shopping for Coats</title>
<content type='application/x-shockwave-flash'
src='http://www.youtube.com/v/ZTUVgYoeN_b?f=gdata_standard...'/>
<link rel='alternate' type='text/html'
href='http://www.youtube.com/watch?v=ZTUVgYoeN_b'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.responses'
type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/responses?v=2'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.ratings'
type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/ratings?v=2'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.complaints'
type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/complaints?v=2'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.related'
type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/related?v=2'/>
<link rel='http://gdata.youtube.com/schemas/2007#mobile'
type='text/html' href='http://m.youtube.com/details?v=ZTUVgYoeN_b'/>
<link rel='self' type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/standardfeeds/top_rated/v/ZTUVgYoeN_b?v=2'/>
<author>
<name>andyland74</name>
<uri>http://gdata.youtube.com/feeds/api/users/andyland74</uri>
</author>
<media:group>
<media:title type='plain'>Shopping for Coats</media:title>
<media:description type='plain'>
What could make for more exciting video?
</media:description>
<media:keywords>Shopping, parkas</media:keywords>
<yt:duration seconds='79'/>
<yt:videoid>ZTUVgYoeN_b</yt:videoid>
<media:player url='http://www.youtube.com/watch?v=ZTUVgYoeN_b'/>
<media:credit role='uploader' scheme='urn:youtube'>andyland74</media:credit>
<media:category label='People'
scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>People
</media:category>
<media:content
url='http://www.youtube.com/v/ZTUVgYoeN_b?f=gdata_standard...'
type='application/x-shockwave-flash' medium='video'
isDefault='true' expression='full' duration='215' yt:format='5'/>
<media:content
url='rtsp://rtsp2.youtube.com/ChoLENy73bIAEQ1kgGDA==/0/0/0/video.3gp'
type='video/3gpp' medium='video'
expression='full' duration='215' yt:format='1'/>
<media:content
url='rtsp://rtsp2.youtube.com/ChoLENy73bIDRQ1kgGDA==/0/0/0/video.3gp'
type='video/3gpp' medium='video'
expression='full' duration='215' yt:format='6'/>
<media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/2.jpg'
height='97' width='130' time='00:00:03.500'/>
<media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/1.jpg'
height='97' width='130' time='00:00:01.750'/>
<media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/3.jpg'
height='97' width='130' time='00:00:05.250'/>
<media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/0.jpg'
height='240' width='320' time='00:00:03.500'/>
</media:group>
<yt:statistics viewCount='383290' favoriteCount='7022'/>
<gd:rating min='1' max='5' numRaters='14763' average='4.93'/>
<georss:where>
<gml:Point>
<gml:pos>21.37124437061831 -157.87353515625</gml:pos>
</gml:Point>
</georss:where>
<gd:comments>
<gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/comments'
countHint='9416'/>
</gd:comments>
</entry>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你对类别标签的看法是对的......做了一些奇怪的事情,当我复制并粘贴谷歌的youtube xml代码时注意到这一点,该代码被注册为文本,因为它位于
而不是他们所拥有的:
但我想这并不能真正帮助您解决问题除非您愿意先修复 XML,否则会出现问题。不过,我已经使用 jQuery 成功解析了相同的提要。它可能是您最初的
$.get
代码,如果您还没有尝试使用$.getJSON
...如果您仍然需要,请让我看看您的其余代码帮助。否则,这就是我一直在使用的,并且它确实有效(据我所知)。我在网站中使用它,而不是在 Adobe AIR 中,但如果您在 AIR 中使用 jQuery,那么 jQuery 就是 jQuery,这应该不重要(恕我直言,这是 jQuery 最好的部分)。这是我的代码:
上面的代码读取包含所有上传视频的任何一个用户的提要。希望这有帮助!
You're right about the category tag...something weird was done, I noticed this when I copied and pasted google's youtube xml code that is registered as text because it is inside of
<pre style="word-wrap: break-word; white-space: pre-wrap;">[XML HERE]</pre>
into an XML parser; it too had trouble reading the category tags, which appeared to be improperly closed (even though they are). I have a feeling that to fix the xml, a space between the forward slash and greater than sign would be necessary, ie<category scheme='[scheme_url]' term='[term_name]' />
instead of what they have:
<category scheme='[scheme_url]' term='[term_name]'/>
But I guess that doesn't really doesn't help you solved your problem unless you are willing to fix the XML first. However, I've used jQuery to parse the same feeds successfully. It could be your initial
$.get
code, try using$.getJSON
if you aren't already...let me see the remainder of your code if you still need help.Otherwise, here's what I've been using, and it does work (as far as I know). I am using it in a website and not Adobe AIR, but if you're using jQuery in AIR, jQuery is jQuery and it shouldn't matter (that's the best part about jQuery, imho). Here's my code:
The code above reads any one user's feed containing all uploaded videos. Hope this helps!