2012-02-14 74 views
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個。

+0

試試這個xpath'// entry/category/@ term'它會以這種方式給你所有關鍵詞'kw1','kw2','kw3','kw4','kw5'。 – RanRag 2012-02-14 19:07:14

+0

感謝您的回覆。我最初嘗試這樣做,除非我有兩個問題。首先,它返回類別類型術語,其次我實際上不知道如何使它返回每個關鍵字。目前它只返回第一個。 – Predz 2012-02-15 02:45:59

+0

要獲得關鍵工作類別,請嘗試'//entry/category[contains(@scheme,'keywords.cat')]/@ term'。如果您遇到命名空間問題,請嘗試:'//*[local-name()='entry']/*[local-name()='category'][contains(@scheme,'keywords.cat' )]/@ term' – 2012-02-15 03:11:30

回答

1

感謝回覆的人。我自己已經剽竊了一些看起來很有用的東西

public static ArrayList getTags(String id) throws IOException, ParserConfigurationException, XPathExpressionException, SAXException { 
    ArrayList<String> tags = new ArrayList<String>(); 

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder = factory.newDocumentBuilder(); 
    Document doc = builder.parse("https://gdata.youtube.com/feeds/api/videos/" + id); 
    NodeList nl = doc.getElementsByTagName("category"); 

    for (int i = 0; i<nl.getLength(); i++) { 
     String kwCheck = "http://gdata.youtube.com/schemas/2007/keywords.cat"; 
     if (kwCheck.equals(nl.item(i).getAttributes().getNamedItem("scheme").getNodeValue())) { 
      String kw = nl.item(i).getAttributes().getNamedItem("term").getNodeValue();  
      tags.add(kw); 
     } 
    } 

    return tags; 
} 

這隻會返回關鍵字,但可能會對某些內容進行整理。你們中的任何人看到這個方法的任何問題?再次感謝