Groovy XmlSlurper 与 XmlParser [英] Groovy XmlSlurper vs XmlParser

查看:50
本文介绍了Groovy XmlSlurper 与 XmlParser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个主题上搜索了一段时间,也找到了一些结果,我在帖子末尾提到了这些结果.谁能帮我准确回答下面列出的三个问题?

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. 对于哪些用例使用 XmlSluper 比 XmlParser 更有意义,反之亦然(从 API/语法的易用性角度来看)?

  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)?

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

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

哪个处理 xml 的速度更快?

which one processes the xml faster?

情况a.当我必须读取 xml 中的几乎所有节点时?

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

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

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

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

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

前提是 xml 文档不是微不足道的(具有 xml 的深度和大小).

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

资源:

http://www.tutkiun.com/2009/10/xmlparser-and-xmlslurper.html 状态:

XMLParser 和 XMLSlurper 的区别:

Difference between XMLParser and XMLSlurper:

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

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 存储解析文档后的中间结果.但是在另一方面,

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

XMLSlurper 在处理 XML 后不存储内部结果文件.

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.

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

groovy 文档(XmlParserXmlSlurper) 并且 groovy 的站点很好地解释了它们 (这里此处)但在解释上述问题方面做得不好.

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

推荐答案

XmlSlurper 和 XmlParser 的最大区别在于 Parser 将创建类似于 DOM 的东西,而 Slurper 仅在真正需要时才尝试创建结构,因此使用路径,被懒惰地评估.对于用户来说,两者看起来非常平等.不同之处在于解析器结构只评估一次,slurper 路径可以按需评估.按需在这里可以理解为内存效率更高但速度更慢".最终这取决于你做了多少路径/请求.例如,如果您只想知道 XML 某个部分中属性的值,然后再完成它,XmlParser 仍将处理所有内容并在准 DOM 上执行您的查询.因为会创建很多对象,内存和 CPU 会消耗掉.XmlSlurper 不会创建对象,从而节省内存和 CPU.如果您无论如何都需要文档的所有部分,那么 slurper 就失去了优势,因为它至少会创建与解析器一样多的对象.

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.

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

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.

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

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.

上面的介绍也解释了什么是更高效的内存,问题(2).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.

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

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.

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

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.

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

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.

这篇关于Groovy XmlSlurper 与 XmlParser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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