简体   繁体   English

你何时以及为什么要使用Apache commons-digester?

[英]When and why would you use Apache commons-digester?

在用java输入和输出xml的所有库中,在哪些情况下,commons-digester是首选工具?

From the digester wiki 来自沼气池维基

Why use Digester? 为什么要使用Digester?

Digester is a layer on top of the SAX xml parser API to make it easier to process xml input. Digester是SAX xml解析器API之上的一个层,可以更轻松地处理xml输入。 In particular, digester makes it easy to create and initialise a tree of objects based on an xml input file. 特别是,digester使得基于xml输入文件创建和初始化对象树变得容易。

The most common use for Digester is to process xml-format configuration files, building a tree of objects based on that information. Digester最常见的用途是处理xml格式的配置文件,根据该信息构建对象树。

Note that digester can create and initialise true objects, ie things that relate to the business goals of the application and have real behaviours. 请注意,digester可以创建和初始化真实对象,即与应用程序的业务目标相关并具有真实行为的事物。 Many other tools have a different goal: to build a model of the data in the input XML document, like a W3C DOM does but a little more friendly. 许多其他工具有不同的目标:在输入XML文档中构建数据模型,就像W3C DOM一样,但更友好一些。

and

And unlike tools that generate classes, you can write your application's classes first, then later decide to use Digester to build them from an xml input file. 与生成类的工具不同,您可以先编写应用程序的类,然后再决定使用Digester从xml输入文件构建它们。 The result is that your classes are real classes with real behaviours, that happen to be initialised from an xml file, rather than simple "structs" that just hold data. 结果是你的类是具有真实行为的真实类,恰好是从xml文件初始化的,而不是仅仅保存数据的简单“结构”。

As an example of what it's NOT used for: 作为它不用于的内容的一个例子:

If, however, you are looking for a direct representation of the input xml document, as data rather than true objects, then digester is not for you; 但是,如果您正在寻找输入xml文档的直接表示,作为数据而不是真实对象,那么消化器不适合您; DOM, jDOM or other more direct binding tools will be more appropriate. DOM,jDOM或其​​他更直接的绑定工具将更合适。

So, digester will map XML directly into java objects. 因此,digester会将XML直接映射到java对象中。 In some cases that's more useful than having to read through the tree and pull out options. 在某些情况下,这比阅读树和提取选项更有用。

My first take would be "never"... but perhaps it has its place. 我的第一次采取将是“从不”......但也许它有它的位置。 I agree with eljenso that it has been surpassed by competition. 我同意eljenso的说法,它已被竞争所超越。

So for good efficient and simple object binding/mapping, JAXB is much better, or XStream . 因此,对于高效且简单的对象绑定/映射, JAXB要好得多,或者是XStream Much more convenient and even faster. 更方便,更快捷。

EDIT 2019: also, Jackson XML , similar to JAXB in approach but using Jackson annotations 编辑2019:同样, 杰克逊XML ,类似于JAXB的方法,但使用杰克逊注释

If you want to create and intialize "true" objects from XML, use a decent bean container, like the one provided by Spring. 如果要从XML创建和初始化“true”对象,请使用类似于Spring提供的容器。

Also, reading in the XML and processing it yourself using XPath, or using Java/XML binding tools like Castor, are good and maybe more standard alternatives. 另外,读取XML并使用XPath自己处理它,或使用像Castor这样的Java / XML绑定工具,这些都是很好的,也许是更标准的替代方案。

I have worked with the Digester when using Struts, but it seems that it has been surpassed by other tools and frameworks for the possible uses it has. 我在使用Struts的时候曾与Digester合作过,但似乎它已被其他工具和框架所超越,以满足它的可能用途。

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

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