使用 dom 解析 gdata xml

发布于 2025-01-05 11:15:32 字数 1796 浏览 5 评论 0原文

我正在寻找一种从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椵侞 2025-01-12 11:15:32

感谢人们的回复。我自己破解了一些东西,似乎可以解决这个问题

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

,这只返回关键字,但可能需要一些整理。你们中有人发现这种方法有任何问题吗?再次感谢

Thanks for replies people. I have hacked something out myself that seems to do the trick

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

This returns keywords only but could probably do with some tidying up. Any of you see any issues with this method ? Thanks again

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文