1
我正在尋找一種方法從YouTube視頻gdata中獲取關鍵字。gdata xml使用dom解析
的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個。
試試這個xpath'// entry/category/@ term'它會以這種方式給你所有關鍵詞'kw1','kw2','kw3','kw4','kw5'。 – RanRag 2012-02-14 19:07:14
感謝您的回覆。我最初嘗試這樣做,除非我有兩個問題。首先,它返回類別類型術語,其次我實際上不知道如何使它返回每個關鍵字。目前它只返回第一個。 – Predz 2012-02-15 02:45:59
要獲得關鍵工作類別,請嘗試'//entry/category[contains(@scheme,'keywords.cat')]/@ term'。如果您遇到命名空間問題,請嘗試:'//*[local-name()='entry']/*[local-name()='category'][contains(@scheme,'keywords.cat' )]/@ term' – 2012-02-15 03:11:30