xml Groovy XmlSlurper 与 XmlParser

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7558019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:58:10  来源:igfitidea点击:

Groovy XmlSlurper vs XmlParser

xmlgroovyxml-parsingxmlslurper

提问by kdabir

I searched for a while on this topic and found some results too, which I am mentioning at the end of post. Can someone help me precisely answer these three questions for the cases listed below them?

我在这个主题上搜索了一段时间,也找到了一些结果,我在文章末尾提到了这些结果。有人可以帮我准确回答下面列出的三个问题吗?

  1. For which use-cases using XmlSluper makes more sense than XmlParser and vice-versa (from point of view ease of use of API/Syntax)?

  2. Which one is more memory efficient? (looks like Slurper)

  3. which one processes the xml faster?

  1. 对于哪些用例使用 XmlSluper 比 XmlParser 更有意义,反之亦然(从 API/语法的易用性角度来看)?

  2. 哪个内存效率更高?(看起来像 Slurper)

  3. 哪个处理 xml 更快?

Case a. when I have to read almost all nodes in the xml?

案例一。当我必须读取 xml 中的几乎所有节点时?

Case b. when I have to read just few nodes (like using gpath expression)?

情况 B. 当我只需要读取几个节点时(比如使用 gpath 表达式)?

Case c. when I have to update/transform the xml?

案例 C. 当我必须更新/转换 xml 时?

provided the xml document is not trivial one (with level of depths and size of the xml).

假设 xml 文档不是微不足道的(具有 xml 的深度和大小级别)。

Resources:

资源

http://www.tutkiun.com/2009/10/xmlparser-and-xmlslurper.htmlstates :

http://www.tutkiun.com/2009/10/xmlparser-and-xmlslurper.html指出:

Difference between XMLParser and XMLSlurper:

There are similarities between XMLParser and XMLSlurper when used for simple reading but when we use them for advanced reading and when processing XML documents in other formats there are differences between two.

XMLParser stores intermediate results after parsing documents. But on the other hand,

XMLSlurper does not stores internal results after processing XML documents.

The real, fundamental differences become apparent when processing the parsed information. That is when processing with direct in-place data manipulation and processing in a streaming scenario.

XMLParser 和 XMLSlurper 的区别:

XMLParser 和 XMLSlurper 在用于简单阅读时有相似之处,但当我们将它们用于高级阅读和处理其他格式的 XML 文档时,两者之间存在差异。

XMLParser 在解析文档后存储中间结果。但另一方面,

XMLSlurper 在处理 XML 文档后不存储内部结果。

在处理解析的信息时,真正的、根本的差异变得明显。那就是在流场景中使用直接就地数据操作和处理进行处理时。

http://groovy.dzone.com/news/john-wilson-groovy-and-xml

http://groovy.dzone.com/news/john-wilson-groovy-and-xml

The groovy doc (XmlParser, XmlSlurper) and the groovy's site explains them well (hereand here)but does not do a great job in explaining the aforementioned question.

groovy 文档(XmlParserXmlSlurper)和 groovy 的网站很好地解释了它们(这里这里),但在解释上述问题方面做得并不好。

回答by blackdrag

The big difference between XmlSlurper and XmlParser is that the Parser will create something similar to a DOM, while Slurper tries to create structures only if really needed and thus uses paths, that are lazily evaluated. For the user both can look extremely equal. The difference is more that the parser structure is evaluated only once, the slurper paths may be evaluated on demand. On demand can be read as "more memory efficient but slower" here. Ultimately it depends how many paths/requests you do. If you for example want only to know the value of an attribute in a certain part of the XML and then be done with it, XmlParser would still process all and execute your query on the quasi DOM. In that a lot of objects will be created, memory and CPU spend. XmlSlurper will not create the objects, thus save memory and CPU. If you need all parts of the document anyway, the slurper looses the advantage, since it will create at least as many objects as the parser would.

XmlSlurper 和 XmlParser 之间的最大区别在于解析器将创建类似于 DOM 的东西,而 Slurper 仅在真正需要时才尝试创建结构,因此使用路径,这些路径被懒惰地评估。对于用户来说,两者看起来非常平等。不同之处在于解析器结构只评估一次,slurper 路径可以按需评估。按需在这里可以理解为“内存效率更高但速度更慢”。最终这取决于你做了多少路径/请求。例如,如果您只想知道 XML 特定部分中属性的值,然后完成它,XmlParser 仍将处理所有内容并在准 DOM 上执行您的查询。其中会创建很多对象,内存和 CPU 花费。XmlSlurper 不会创建对象,从而节省内存和 CPU。

Both can do transforms on the document, but the slurper assumes it being a constant and thus you would have to first write the changes out and create a new slurper to read the new xml in. The parser supports seeing the changes right away.

两者都可以对文档进行转换,但 slurper 假定它是一个常量,因此您必须首先写出更改并创建一个新的 slurper 以读取新的 xml。解析器支持立即查看更改。

So the answer to question (1), the use case, would be, that you use the parser if you have to process the whole XML, the slurper if only parts of it. API and syntax don't really play much a role in that. The Groovy people try to make those two very similar in user experience. Also you would prefer the parser over the slurper if you want to make incremental changes to the XML.

因此,问题 (1) 的答案是用例,如果您必须处理整个 XML,则使用解析器,如果只处理其中的一部分,则使用解析器。API 和语法在这方面并没有真正发挥多大作用。Groovy 人试图使这两者在用户体验上非常相似。如果您想对 XML 进行增量更改,您也更喜欢解析器而不是 slurper。

That intro above also explains then what is more memory efficient, question (2). The slurper is, unless you read in all anyway, then the parser may, but I don't have actual numbers about how big the difference is then.

上面的介绍也解释了什么是更高效的内存,问题(2)。slurper 是,除非你无论如何都阅读,然后解析器可能会,但我没有关于差异有多大的实际数字。

Also question (3) can be answered by the intro. If you have multiple lazy evaluated paths, you have to eval again, then this can be slower than if you just navigate an existing graph like in the parser. So the parser can be faster, depending on your usage.

问题(3)也可以通过介绍来回答。如果您有多个惰性求值路径,则必须再次求值,这可能比仅在解析器中导航现有图形要慢。因此解析器可以更快,具体取决于您的使用情况。

So I would say (3a) reading almost all nodes itself makes not much of a difference, since then the requests are the more determining factor. But in case (3b) I would say that the slurper is faster if you just have to read a few nodes, since it will not have to create a complete structure in memory, which in itself already costs time and memory.

所以我会说(3a)读取几乎所有节点本身并没有太大区别,因为那时请求是更具决定性的因素。但是在情况 (3b) 中,如果您只需要读取几个节点,我会说 slurper 会更快,因为它不必在内存中创建完整的结构,而这本身已经消耗了时间和内存。

As for (3c)... these days both can update/transform the XML, which is faster is actually more linked to how many parts of the xml you have to change. If many parts I would say the parser, if not, then maybe the slurper. But if you want to for example change an attribute value from "Fred" to "John" with the slurper, just to later query for this "John" using the same slurper, it won't work.

至于(3c)……现在两者都可以更新/转换 XML,这更快实际上更多地与您必须更改的 xml 的多少部分有关。如果有很多部分我会说解析器,如果没有,那么也许是slurper。但是,例如,如果您想使用 slurper 将属性值从“Fred”更改为“John”,只是为了稍后使用相同的 slurper 查询此“John”,那么它将不起作用。

回答by srinivasan

I Will Give You A Crisp Answers:

我会给你一个清晰的答案:

*XML Parser Is Faster Then XML Slurper.
*XML Slurper Consumes Lesser Memory Than XML Parser.
*XML Parser Can Parse And Update The XML Simultaneously.
*For XML Slurper You Need To MarkupBuild The XML's After Each Update You Make.
*When You Want To use Path Expressions XML Slurper Would Be Better Than Parser.
*For Reading Almost All Nodes XML Parser Would Be Fine

*XML 解析器比 XML Slurper 更快。
*XML Slurper 比 XML Parser 消耗更少的内存。
*XML 解析器可以同时解析和更新 XML。
*对于 XML Slurper,您需要在每次更新后标记构建 XML。
*当你想使用路径表达式时,XML Slurper 会比解析器更好。
*用于读取几乎所有节点 XML 解析器会很好

Hope It Helps

希望能帮助到你