使用 dom 解析 gdata xml
我正在寻找一种从 youtube 视频 gdata 中获取关键字的方法。
xml 看起来如下所示:
<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
<id>http://gdata.youtube.com/feeds/api/videos/vidid</id>
<category scheme='http://gdata.youtube.com/schemas/2007/categories.cat' term='Comedy' label='Comedy'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='kw1'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='kw2'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='kw3'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='kw4'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='kw5'/>
<title type='text'>vid title</title>
...
</entry>
我在 ... 所在的位置剪掉了一些内容,因此我可以使用以下代码获取标题:
public static String getTitle(String id) throws IOException, ParserConfigurationException, XPathExpressionException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("https://gdata.youtube.com/feeds/api/videos/" + id);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//entry/title/text()");
Object result = expr.evaluate(doc, XPathConstants.STRING);
String title = (String) result;
return title;
}
有没有某种方法可以修改它以获取关键字? 我应该提一下,可以有任意数量的关键字,而不仅仅是上面所示的 5 个。
I'm looking for a way to get the keywords from youtube video gdata.
The xml Looks something like the following:
<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
<id>http://gdata.youtube.com/feeds/api/videos/vidid</id>
<category scheme='http://gdata.youtube.com/schemas/2007/categories.cat' term='Comedy' label='Comedy'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='kw1'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='kw2'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='kw3'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='kw4'/>
<category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='kw5'/>
<title type='text'>vid title</title>
...
</entry>
I cut some stuff out where the ... is, so I can get the title using the following code:
public static String getTitle(String id) throws IOException, ParserConfigurationException, XPathExpressionException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("https://gdata.youtube.com/feeds/api/videos/" + id);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//entry/title/text()");
Object result = expr.evaluate(doc, XPathConstants.STRING);
String title = (String) result;
return title;
}
Is there some way to modify this to get the keywords too?
I should mention, there can be any number of keywords, not just 5 as shown above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢人们的回复。我自己破解了一些东西,似乎可以解决这个问题
,这只返回关键字,但可能需要一些整理。你们中有人发现这种方法有任何问题吗?再次感谢
Thanks for replies people. I have hacked something out myself that seems to do the trick
This returns keywords only but could probably do with some tidying up. Any of you see any issues with this method ? Thanks again