gdata xml使用dom解析

问题描述:

我正在寻找一种方法从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

感谢回复的人。我自己已经剽窃了一些看起来很有用的东西

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; 
} 

这只会返回关键字,但可能会对某些内容进行整理。你们中的任何人看到这个方法的任何问题?再次感谢