【问题标题】:gdata xml parsing with dom使用 dom 解析 gdata xml
【发布时间】:2012-02-15 11:28:51
【问题描述】:

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

【问题讨论】:

  • 试试这个 xpath //entry/category/@term 它将以这种方式为您提供所有关键字'kw1', 'kw2', 'kw3', 'kw4', 'kw5'
  • 感谢您的回复。我最初尝试这样做,除了我有两个问题。首先,它返回类别类型术语,其次我实际上不知道如何让它返回每个关键字。目前它只返回第一个。
  • 要仅获取密钥类别,请尝试//entry/category[contains(@scheme,'keywords.cat')]/@term。如果您遇到命名空间问题,请尝试://*[local-name()='entry']/*[local-name()='category'][contains(@scheme,'keywords.cat')]/@term

标签: java xml parsing gdata


【解决方案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;
}

这仅返回关键字,但可能需要进行一些整理。你们中有人看到这种方法有什么问题吗?再次感谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 2013-01-31
    • 2013-07-18
    相关资源
    最近更新 更多