SAX 解析器与 XMLPull 解析器

发布于 2024-11-04 04:10:34 字数 353 浏览 5 评论 0原文

我了解 SAX 解析器与 XMLPull 解析器的工作方式之间的区别。 事实上,这里有一个很好的解释:

http:// www.firstobject.com/xml-reader-sax-vs-xml-pull-parser.htm 这篇文章有点以 .NET 为中心,但概念是适用的。

虽然我同意作者的观点,即 Pull 解析器更容易使用,但我很困惑哪种类型的解析器在哪种情况下会更好。 如果有人能阐明并指出我更多的阅读内容,我将不胜感激。

谢谢。

I understand the difference between how the SAX parser works vs the XMLPull parser.
In fact there's a pretty good explanation here:

http://www.firstobject.com/xml-reader-sax-vs-xml-pull-parser.htm
The article is a bit .NET centric but the concepts apply.

While I agree with the author's opinion that the Pull parser is easier to work with, I'm pretty confused as to which type of parser would be better in which situations.
If anyone could shed any light and point me to some more reading I would appreciate it.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

祁梦 2024-11-11 04:10:34

我发现他们俩都很烂。 (我有一个更好的解决方案建议)

您应该使用基于简单注释的 XML 库。我喜欢它并将其用于我的所有项目。如果您阅读本教程,那么我想您会发现它将能够用更少的代码更快地完成您想要的一切。 (因此不易出现错误)在内部,该库使用您要求的那些解析器来完成繁重的工作。

然后,您可以阅读我的博客文章,将其包含在如果需要的话,可以选择一个 Android 项目。 (它适用于至少 1.5 版以上的所有 Android 版本,这意味着基本上适用于所有人)

I find that they both suck. (And I have a better solution to suggest)

You should use the Simple annotation based XML library. I love it and use it for all of my projects. If you read through the tutorial then I think you will find that it will be able to do everything that you want and much faster and with less code. (Thus being less bug prone) Internally the library uses those parsers that you were asking about to do the heavy lifting.

You can then read my blog post on including it in an Android project if you want. (It will work in every version of Android from atleast 1.5 up which means for everybody basically)

聚集的泪 2024-11-11 04:10:34

这完全取决于情况,例如,如果 xml 文件确实很大,那么您就不能选择 DOM 解析器,因为它们会首先将文件放入内存,然后对其进行解析,我发现解析大小为 n 的文件需要7n内存空间。在这种情况下,您应该选择 SAX 解析器,它轻便并且消耗更少的内存。

第二种情况是当文件不是很大时,在这种情况下您可以使用 XML pull 解析器,因为在这种情况下您将完全控制 xml,您可以跳过 SAX 中不可能的任何解析周期。因此,如果您要查找的标签是文件中的第一个标签,那么为什么要查找整个文件。

据我所知,如果您只考虑小文件的速度,请使用 XML pull 解析器,如果文件很大并且您想解析全部内容,请使用 SAX。

It totally depends on the situation for e.g If the xml file is really large than you can't opt for DOM parsers as they will first bring the file in to memory and then it will be parsed and i found that parsing a file of size n requires 7n memory space. In this case you should opt for SAX parser its light and will consume less memory.

Second case is when the file is not really large, in this case you can go for XML pull parser because in this you will have full control on the xml you can skip the parsing cycle any where that is not possible in SAX. So if the tag you are looking for is the first one in the file then why would you go for whole file.

So as far as i know if you consider only speed with small file go with XML pull parser and If the file is large and you want to parse it all then go with SAX.

策马西风 2024-11-11 04:10:34

两个解析器在内存/时间方面基本上是相同的。
唯一的事情是,使用拉解析器,您可以拉出诸如 startelement 和 endelement 之类的事件,并且只关注您想要的事件。

与 android sax 解析器一样,您别无选择,只需将代码放在您想要的位置,但必须包含所有事件。

此处是一个链接,您可以参考该链接进行进一步阅读。

Both the parsers are basically the same memory/time wise.
The only thing being that with pull parser you can pull out the events like startelement and endelement and only heed the ones that you want to.

where as with android sax parsers, you have no choice, you just put code where you want to but you have to include all the events.

here is a link which you can refer for further reading.

蓝海 2024-11-11 04:10:34

我发现 SAX 模型在一种特定情况下更容易使用:您将使用自定义数据结构构建整个文档(或至少其主要部分)的自己的内存中表示。 (如果您不特别关注数据结构,那么 DOM 解析器已经做到了这一点。)

I find the SAX model easier to work with in one specific situation: where you are going to build your own in-memory representation of the entire document (or at least major portions of it) with custom data structures. (If you aren't particular about the data structure, then the DOM parser already does this.)

Pull 和 Sax 的相似之处在于,它们都是低级流方法,比 DOM 更快、内存效率更高,但 Pull 比 SAX 有一些优势:

Pull 比 SAX 更容易实现,因为您不必维护解析器的状态(使用附加变量能够知道解析器当前位于 XML 树中的位置)。 Pull 解析器代码中的嵌套循环或多或少会与文档的 XML 层次结构相匹配,因此我认为 Pull 解析器代码也比 SAX 解析器代码更具可读性。

使用拉解析器代码,您可以跳过不想解析的整个块,因此它也比 SAX 更有效,SAX 总是提取所有节点的主要信息。使用拉式解析器,如果您获取了所需的信息,您还可以随时停止解析,而这对于 SAX 来说是不可能的。

此外,您还可以使用拉解析器来实现 SAX 解析器。相反的情况是不可能的。

出于所有这些原因,我相信拉解析器在所有情况下都优于 SAX,但是与 SAX 一样,正确实现它并不容易,您必须小心。如果您不需要 pull 和 SAX 的低级速度优势并且您的 XML 是干净的,那么您始终可以使用更高级别的解析库,例如 简单为您完成艰苦的工作。

Pull and Sax are similar in the way that they are both low-level streaming approaches which are faster and more memory efficient than DOM, but pull has a few advantages over SAX:

Pull is easier to implement than SAX because you don't have to maintain the state of your parser (using additional variables to be able to know in which place your parser currently is in the XML tree). The nested loops in your pull parser code will more or less match the XML hierarchy of your document so I think the Pull parser code is also more readable than the SAX parser code.

With pull parser code you can skip entire blocks that you don't want to parse so it's also more efficient than SAX which always extracts the main information of all nodes. Using a pull parser, you can also stop the parsing at any moment if you fetched the information you wanted, which is not possible with SAX.

Also, you can implement a SAX parser using a pull parser. The opposite is not possible.

For all these reasons I believe the pull parser is superior to SAX in all situations, however like SAX it's not trivial to implement properly and you have to be careful. If you don't need the low-level speed benefits of pull and SAX and your XML is clean, you can always use a higher-level parsing library like Simple to do the hard work for you.

扎心 2024-11-11 04:10:34

我在使用 SAX 而不是 XMLPullParser 时发现了更好、更高效的输出...我的场景是解析 XML 标签下的属性,我可以轻松完成并将其顺利插入数据库...我认为这取决于情况,当我需要在 XML 文件上写入,我更喜欢 DOM 解析器...

public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        currentElement = true;
        db = new DatabaseHelper(thecontext);
        if (qName.equals("Asa.Amms.Data.Entity.User")) {
            int length = attributes.getLength();
            for (int i = 0; i < length; i++) {
                String name = attributes.getQName(i);
                if (name.equals("Id")) {
                    id = Integer.parseInt(attributes.getValue(i));
                }
                if (name.equals("Login")) {
                    LoginID = attributes.getValue(i).toString();
                }
                if (name.equals("Name")) {
                    Name = attributes.getValue(i).toString();
                }
                if (name.equals("Password")) {
                    Password = attributes.getValue(i).toString();
                }
                if (name.equals("ProgramOfficerId")) {
                    user_ProgramOfficerId = Integer.parseInt(attributes.getValue(i).toString());
                }
            }
            Log.i("Baal dhukbe", id + LoginID + Name + Password);

            db.insertUser(id, LoginID, Name, Password, user_ProgramOfficerId);
        }
}

I found better and more efficient output while using SAX rather than XMLPullParser... My scenario is to parse the attributes under a XML tag, i could do it easily and insert it into Database smoothly... I think it depends on situations, when i need to write on a XML file i prefer DOM Parser...

public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        currentElement = true;
        db = new DatabaseHelper(thecontext);
        if (qName.equals("Asa.Amms.Data.Entity.User")) {
            int length = attributes.getLength();
            for (int i = 0; i < length; i++) {
                String name = attributes.getQName(i);
                if (name.equals("Id")) {
                    id = Integer.parseInt(attributes.getValue(i));
                }
                if (name.equals("Login")) {
                    LoginID = attributes.getValue(i).toString();
                }
                if (name.equals("Name")) {
                    Name = attributes.getValue(i).toString();
                }
                if (name.equals("Password")) {
                    Password = attributes.getValue(i).toString();
                }
                if (name.equals("ProgramOfficerId")) {
                    user_ProgramOfficerId = Integer.parseInt(attributes.getValue(i).toString());
                }
            }
            Log.i("Baal dhukbe", id + LoginID + Name + Password);

            db.insertUser(id, LoginID, Name, Password, user_ProgramOfficerId);
        }
}
提赋 2024-11-11 04:10:34

我建议使用 XmlPullParser 一个.. Sax 解析器没有从我的测试中的提要中检索标签.. xmlpullparser 很容易做到这一点 =)
也取决于你的喜好

i'd recommend using the XmlPullParser one.. the Sax parser didn't retrieve the tag from a feed in my test.. xmlpullparser did that easily =)
also depends on your preferences also

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