简体   繁体   English

IN java,commons-Digester 如何处理输入的 XML 文件?

[英]IN java, how a commons-Digester process an input XML file?

I am new to Java and I came across a statement in a Java project which says:我是 Java 的新手,我在 Java 项目中看到一个声明:

Digester digester = DigesterLoader.createDigester(getClass()
                .getClassLoader().getResource("rules.xml"));

rules.xml file contains various patterns and every pattern has different attributes like classname, methodname and some another properties. rules.xml 文件包含各种模式,每个模式都有不同的属性,如类名、方法名和其他一些属性。

i googled about digester but couldn't found anything useful that could help me with the statement above.我用谷歌搜索了 digester 但没有找到任何有用的东西可以帮助我完成上面的陈述。 can anyone just tell me what are the steps followed in executing above statement?谁能告诉我执行上述声明的步骤是什么? In fact what is the advantage of this XML stuff?其实这个XML的东西有什么好处呢?

swapnil, as a user of Digester back in my Struts days I can honestly say it's tricky to learn/debug. swapnil,作为我在 Struts 时代使用 Digester 的用户,我可以诚实地说它很难学习/调试。 It's a tough library to familiarize yourself with, essentially you are setting up event handlers for certain elements kinda like a SAX parser (in fact it's using SAX in behind the scenes).这是一个很难让自己熟悉的库,本质上,您是在为某些元素设置事件处理程序,有点像 SAX 解析器(实际上它在幕后使用 SAX)。 So you feed a rules engine some XPath for nodes you are interested in and setup rules which will instantiate, and set properties on some POJOs with data it finds in the XML file.因此,您为您感兴趣的节点提供一些 XPath 的规则引擎,并设置将实例化的规则,并使用它在 XML 文件中找到的数据设置某些 POJO 的属性。

Great idea, and once you get used to it it's good, however if you have an xsd for your input xml file I'd sooner recommend you use JAXB.好主意,一旦你习惯了它就很好,但是如果你有一个 xsd 作为输入 xml 文件,我会尽快建议你使用 JAXB。

The one thing that is nice about Digester is it will only do things with elements you are interested in, so memory footprint ends up being nice and low. Digester 的一个优点是它只会处理您感兴趣的元素,因此 memory 占用空间非常小。

This is the method that's getting called here .这是在这里调用的方法。 Xml is used commonly in Java for configurations, since xml files do not need to be compiled. Xml是Java中常用的配置,因为883812976388文件不需要编译。 Having the same configuration in a java file would mean you have to compile the file.在 java 文件中具有相同的配置意味着您必须编译该文件。

I assume you understand how the rules file is being loaded using the class loader?我假设您了解如何使用 class 加载程序加载规则文件? It's basically looking in the same package as the class itself and creating a URL that gives the file's absolute location.它基本上查找与 class 本身相同的 package 并创建一个URL来提供文件的绝对位置。

As for the Digester, I've not used it but a quick read of this ( http://commons.apache.org/digester/ ) should explain all.至于 Digester,我没有使用过它,但快速阅读一下这个 ( http://commons.apache.org/digester/ ) 应该可以解释一切。

They used it at my last gig and all I remember is that it was extremely slow.他们在我的最后一场演出中使用了它,我只记得它非常慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM