【问题标题】:Android parse XML to get node valueAndroid 解析 XML 以获取节点值
【发布时间】:2012-12-13 07:12:25
【问题描述】:

所以,我写了一些小东西来解析 Youtube 用户的提要。

问题是当我调用getNodeValue() 时返回null。为什么是这样?任何想法或替代方法都表示赞赏,谢谢。

这是我目前做事方式的简要说明:

try {

    Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("https://gdata.youtube.com/feeds/api/users/estudiosabiertostv/uploads");

    NodeList entries = d.getElementsByTagName("entry");

    for(int i = 0; i < entries.getLength(); i++) {

        NodeList children = entries.item(i).getChildNodes();
        YoutubeVideo video = new YoutubeVideo();

        for(int j = 0; j < children.getLength(); j++) {

            Node child = children.item(j);

            if(child.getNodeName().equals("title")) {

                video.title = child.getNodeValue();
                break;
            }
        }
        videos.add(video);
    }
}
catch (Exception e) {

    e.printStackTrace();
}

这是我从中获取 XML 的网址:

https://gdata.youtube.com/feeds/api/users/estudiosabiertostv/uploads

这是一个条目的 sn-p,我正在尝试获取其中的标题

<entry>
    <id>http://gdata.youtube.com/feeds/api/videos/oDbOmTY3gDw</id>
    <published>2012-12-17T08:14:12.000Z</published>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#video"/>
    <title type="text">SEAS - Grupo San Valero - Crea tu propia Navidad (2012)</title>

    <content type="text">Felicitación de Navidad de SEAS, Estudios Abiertos del Grupo San Valero</content>
    <link rel="alternate" type="text/html" href="http://www.youtube.com/watch?v=oDbOmTY3gDw&feature=youtube_gdata"/>
    <author>
        <name>estudiosabiertostv</name>
        <uri>http://gdata.youtube.com/feeds/api/users/estudiosabiertostv</uri>
    </author>
</entry>

【问题讨论】:

  • 在我的脑海中,标题节点应该有一个子节点 - 一个文本节点,并且应该包含实际的文本。
  • @full.stack.ex 你的东西很棒。创建一个答案,我会接受。

标签: android xml gdata


【解决方案1】:

标题节点应该有一个子节点 - 一个文本节点,该节点应该包含实际文本。

另外,请注意碎片:那里可能有几个相邻的文本节点,如下所述:http://www.drillio.com/en/software-development/java/fragmented-xml-text-nodes/

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 2013-12-22
    相关资源
    最近更新 更多