Groovy XmlSlurper vs XmlParser [英] Groovy XmlSlurper vs XmlParser

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

问题描述

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


  1. 使用XmlSluper的用例使更多而不是XmlParser,反之亦然(从API / Syntax的易用性角度来看)?


  2. 哪一个更有效率? (看起来像Slurper)

  3. 哪一个更快地处理xml?


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



案例b。当我必须只读几个节点(如使用gpath表达式)?



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



提供的xml文档不是微不足道的(具有深度级别和xml大小)。 b
$ b

资源

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


XMLParser和XMLSlurper的区别:



当用于$ b $时,XMLParser和XMLSlurper之间有相似之处b简单的阅读,但是当我们使用它们进行高级阅读时,以及
处理其他格式的XML文档时,两者之间存在差异



XMLParser商店解析文档后的中间结果。但另一方面,在
上,

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



处理
解析信息时,真正的根本区别变得明显。这是当处理直接就地数据
操作和处理流场景。


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



Groovy文档( XmlParser XmlSlurper )和groovy的网站(此处这里),但在解释上述问题方面做得不好。

解决方案

XmlSlurper和XmlParser最大的区别在于解析器会创建一些simil ar到DOM,而Slurper只在真正需要的时候才尝试创建结构,因此使用的是被懒惰评估的路径。对于用户来说,两者看起来可以非常平等不同之处在于解析器结构仅评估一次,可以根据需要评估更新器路径。在需求可以被认为是更高的内存效率,但更慢在这里。最终取决于你做了多少路径/请求。例如,如果您只想知道XML特定部分中某个属性的值,然后完成它,则XmlParser仍会处理所有内容并在准DOM上执行您的查询。因为会创建很多对象,内存和CPU花费。 XmlSlurper不会创建对象,从而节省内存和CPU。如果您仍然需要文档的所有部分,则slurper将失去优势,因为它将创建至少与解析器一样多的对象。


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



因此,问题(1)的答案是用例,如果使用解析器if你必须处理整个XML,如果仅仅是部分的话。 API和语法在这方面并没有真正起到很大的作用。 Groovy人试图让这两个用户体验非常相似。如果您想对XML进行增量更改,您也会更喜欢语法分析器。

上面的介绍也解释了什么是更高效的内存,问题(2 )。如果没有读过,那么解析器可能会,但我没有实际的数字,说明差异有多大。



另外问题( 3)可以通过介绍回答。如果您有多个延迟评估路径,则必须再次进行评估,然后这可能会比您在解析器中导航现有图形时慢。因此,解析器可以更快,这取决于您的使用情况。



所以我会说(3a)读取几乎所有的节点本身并没有太大的区别,是更重要的决定性因素。但在(3b)的情况下,如果你只需要读取几个节点,我会说slurper速度更快,因为它不需要在内存中创建一个完整的结构,这本身已经花费了时间和内存。



至于(3c)...现在这两天都可以更新/转换XML,速度更快实际上与更改xml的多少部分有关。如果很多部分我会说解析器,如果不是的话,那么也许就是slurper。但是,如果您想要使用slurper将属性值从Fred更改为John,稍后使用相同的slurper查询此John,则该属性值将不起作用。


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?

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

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

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

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

Resources :

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

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.

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

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.

解决方案

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. Ultimatively 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.

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.

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.

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.

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.

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.

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 vs XmlParser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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