如何从 XML 文件创建对象图?
我有一个 XML 文件。它可能是这样的:
<person>
<name>
<firstname>Joni</firstname>
<lastname>Smith</lastname>
</name>
<born year="1983" day="31" month="01">Finland</born>
... lots of elements ...
</person>
我的目标是创建 Person 类。我怎样才能“自动”做到这一点?我想我已经使用了一些 Maven Castor 插件来从 XML 文件创建一个相当复杂的对象图,而不需要付出太多的努力。但是,我不记得那个插件是什么,甚至不记得我到底是如何使用它的。我也很高兴了解您可能知道的其他(可能更好)工具。
I have an XML file. It could be something like:
<person>
<name>
<firstname>Joni</firstname>
<lastname>Smith</lastname>
</name>
<born year="1983" day="31" month="01">Finland</born>
... lots of elements ...
</person>
My goal is to create class Person. How can I do it "automatically"? I think I have used some maven castor plugin to create a quite complicated object graph from an XML file without a lot of effort. However, I can not remember what that plugin was, and indeed can not remember how did I exactly used it. I am also very happy to learn about other (probably better) tools you might know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我同意使用 JAXB。
从 XML Schema 开始(从 XML Schema 生成类)
您可以使用 JAXB 从 XML Schema 生成 Java 源代码。以下是使用 EclipseLink JAXB (MOXy) 执行此操作的说明:
Java SE 6 附带了 Metro JAXB XJC 编译器,可以在 JDK 安装的 bin 目录中找到它:
Eclipse 中的 Dali 插件也有此支持请参阅有关 JAXB 类生成的部分:
从对象开始
通过您的对象模型,您可能会发现 MOXy JAXB 中基于 XPath 的映射扩展很有用
:与以下演示代码一起使用以处理您的 XML:
有关基于 XPath 的映射的更多信息,请参阅:
对于“born”元素,您可能会发现 JAXB 的 XmlAdapter 很有帮助:
I agree with using JAXB.
Starting from XML Schema (Generate classes from XML Schema)
You can use JAXB to generate Java source code from XML Schema. Below are the instructions for doing this with EclipseLink JAXB (MOXy):
Java SE 6 comes with the Metro JAXB XJC compiler it can be found in the bin directory of your JDK installation:
The Dali plug-in in Eclipse also has this support see section on JAXB class generation:
Starting from Objects
With your object model you may find the XPath based mapping extension in MOXy JAXB useful:
Can be used with the following demo code to work with your XML:
For more information on XPath based mappings see:
For the "born" element you might find JAXB's XmlAdapter helpful:
查看 JAXB。工具有很多,选择最适合您需求的工具。
Look into JAXB. There are many tools, pick the one that best suits your needs.
有几个工具。来自 Apache Commons 的 Digester 就是其中之一。使用起来非常简单。
更新:这是与提到的其他工具的比较在其他答案(xmlbeans,jaxb)中。总之,Digester 是最薄的,适合将 xml 加载到对象中(例如,特别适合“内部”配置文件)。其他工具更面向完整的 xml 对象映射(双向),并且在涉及 xml 模式时特别有用。
There a several tools. Digester, from Apache Commons, is one of them. Quite simple to use.
Update: Here's a comparison with other tools mentioned in other answers (xmlbeans, jaxb). In summary, Digester is the most thin, apt for just loading xml into objects (specially adequate for "internal" config files for example). The other tools are more oriented towards a full xml-object mapping (both directions) and specially useful when xml schemas are involved.
Apache XMLBeans 是一种从 XML 生成类的非常好的方法,并且支持许多高级 XML 功能(例如类型继承)在其他工具中没有得到很好的支持。 XMLBeans 有一个命令行工具,用于生成一个 jar,然后将其包含在项目中,其中包含所有 bean 类以及用于创建和使用 XML 文档的工厂。
Apache XMLBeans is a very good way to generate classes from XML, and supports a lot of advanced XML features (such as type inheritance) not well supported in other tools. XMLBeans has a command line tool used to generate a jar you then include in your project which contains all the bean classes plus factories for creating and consuming XML documents.