如何解析与一些标签冒号的XML? [英] How to parse an XML with colons in some tags?

查看:1167
本文介绍了如何解析与一些标签冒号的XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读关于 XmlPullParser 一些教程在Android中如何解析XML数据。
更具体地讲,我使用的XML从 https://gdata.youtube.com/feeds/api/standardfeeds/top_rated

I've been reading some tutorials on XmlPullParser in Android on how to parse XML data. To be more specific, I'm using the XML from https://gdata.youtube.com/feeds/api/standardfeeds/top_rated

下面我简化从这个进给(我希望在不改变结构)中的一个条目的部分:

Here I simplify part on an entry from this feed (I hope without altering the structure) in:

<entry>
<id>http://gdata.youtube.com/feeds/api/videos/abc45678qwe</id>
[...]
<title type='text'>THE TITLE</title>
[...]
<link rel='alternate' type='text/html' href='https://www.youtube.com/watch?v=abc45678qwe&amp;feature=youtube_gdata'/>
[...]
<media:group>
[...]
<media:title type='plain'>THE TITLE</media:title>
<yt:duration seconds='300'/>
[...]
<yt:videoid>abc45678qwe</yt:videoid>
</media:group>
<gd:rating average='1' max='5' min='1' numRaters='1' rel='http://schemas.google.com/g/2005#overall'/>
<yt:statistics favoriteCount='0' viewCount='11111111'/>
<yt:rating numDislikes='111' numLikes='111'/>
</entry>

我顺利地拿到冠军,并与链接:

I successfully get the title and the link with:

private String[] readEntry(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, null, "entry");
    String title = null;
    String link = null;

    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }

        String name = parser.getName();
        String rel = parser.getAttributeValue(null, "rel");

        if (name.equalsIgnoreCase("title")) {
            title = readTitle(parser);
        } else if (name.equalsIgnoreCase("link")
                && rel.equals("alternate")) {
            link = readLink(parser);
        } else {
            skip(parser);
        }
    }
    return new String[] { title, link };
}

private String readLink(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    String link = "";
    parser.require(XmlPullParser.START_TAG, null, "link");

    link = parser.getAttributeValue(null, "href");
    parser.nextTag();

    parser.require(XmlPullParser.END_TAG, null, "link");

    return link;
}

private String readTitle(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, null, "title");
    String title = readText(parser);
    parser.require(XmlPullParser.END_TAG, null, "title");
    return title;
}

但无论我怎么努力,我无法从获得秒持续时间&LT; YT:持续时间秒='300'/&GT;

显然不能用类似上述方法访问的东西,作为处理命名空间应必需的,但我不知道。因为我有点失去了这一点,任何的建议是pciated多少AP $ P $。谢谢你。

Clearly it can't be accessed with something similar to the above methods, as handling namespaces should be required, but I'm not sure. Since I'm kinda lost on this, any suggestion is much appreciated. Thanks.

====

编辑:我添加什么,我试图进入标记 YT:病程

edit: I'm adding what I tried to enter the tag yt:duration.

我以前添加其他检查跳过(分析器); 。即:

} else if (name.equalsIgnoreCase("yt:")) {
    Utils.logger("i", "entering yt:", TAG);
    readDuration(parser)
}

和我换YTYTYT :病程没有结果结果
另外随着

and I changed "yt:" with "yt", or "yt:duration with no result.
Also with

String namespace = parser.getNamespace();

和不断变化的 name.equalsIgnoreCase ... namespace.equalsIgnoreCase ... 我没有得到日志条目,所以我甚至不有办法试试这个:

and changing name.equalsIgnoreCase... with namespace.equalsIgnoreCase... I don't get the log entry, so I don't even had a way to try this:

private String readDuration(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, "yt", "duration");

    String seconds = parser.getAttributeValue(null, "seconds");
    parser.nextTag();

    parser.require(XmlPullParser.END_TAG, "yt", "duration");

    Utils.logger("i", "duration: " + seconds + " seconds", TAG);
    return seconds;
}

增加作出了关于要求。我不知道这是不够用。

Addition made "on request". I'm not sure it's useful enough.

推荐答案

XmlPullParser 似乎已是名称空间感知的能力,不同的是它必须被明确地设置。每<一个文档href=\"http://www.xmlpull.org/v1/doc/api/org/xmlpull/v1/XmlPullParserFactory.html#setNamespaceAware%28boolean%29\"相对=nofollow> XmlPullParseFactory#setNamespaceAware

XmlPullParser seems to have the ability to be namespace aware, the difference is it has to be explicitly set. Per the documentation of XmlPullParseFactory#setNamespaceAware:

指定由该工厂生产的解析器将提供
  支持XML名称空间。默认的这个值被设置到
  假的。

Specifies that the parser produced by this factory will provide support for XML namespaces. By default the value of this is set to false.

您可能会想尝试该选项。

You might want to try that option.

此外,如我曾尝试通过你的XML与DOM零问题遍历评论中提到,下面是打印所有时间价值的源泉code(只是让你知道,这是要运行作为的Java 程序,而不是在 ADT

Also, as mentioned in the comments I have tried to traverse through your xml with DOM with zero issues, below is the source code of printing all the duration values (just to let you know, this is to be run as a Java program and not within the ADT):

public static void main(String[] args) throws ParserConfigurationException,
            SAXException, IOException {
        InputStream path = new URL(
                "https://gdata.youtube.com/feeds/api/standardfeeds/top_rated")
                .openStream();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(path);
        traverse(document.getDocumentElement());

    }

    public static void traverse(Node node) {
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node currentNode = list.item(i);
            traverse(currentNode);

        }

        if (node.getNodeName().equals("yt:duration")) {
            Element durationElement = (Element) node;
            System.out.println(durationElement.getAttribute("seconds"));
        }

    }

输出我得到:

56
361
225
265
219
220
259
267
376
205
127
308
249
17
162
220
183
298
172
267
204
209

我总是preFER递归(如上)与 DOM ,因为它简化了充分遍历从而提供灵活了。

I always prefer recursion (as above) with DOM as it simplifies the full traversal thereby providing the flexibility too.

如果您想了解更多有关这些元素组合在一起,你可以参考我的文章<一个href=\"http://stackoverflow.com/questions/21534340/using-java-to-parse-xml/21713344#21713344\">here为好。

If you want to know more about grouping these elements together, you can refer to my post here as well.

这篇关于如何解析与一些标签冒号的XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆