使用 XStream 从 XML 文件生成 Java 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12454240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate Java class from XML file, using XStream
提问by Ulisse
I have many xml files and I would like to use XStream to manage them. Is it possible to generate java classes corresponding to my xml files using XStream?
我有很多 xml 文件,我想使用 XStream 来管理它们。是否可以使用 XStream 生成与我的 xml 文件相对应的 java 类?
采纳答案by dertoni
XStreamis a software to serialize and deserialize a Java Object to and from XML. XStream uses Reflection for this. The class of the objects involved has to exist beforehand.
XStream是一种用于将 Java 对象序列化和反序列化为 XML 和从 XML 序列化和反序列化的软件。XStream 为此使用反射。所涉及的对象的类必须事先存在。
JAXBis a binding framework, which too does serialization and deserialization. JAXB has annotations to do this work. Bundled with the framework come tools to generate classes (complete with the already mentioned annotations) from an xsd (<-- declaration that describes how a specific xml document is structured).
JAXB是一个绑定框架,它也做序列化和反序列化。JAXB 有注释来完成这项工作。与框架捆绑在一起的工具是从 xsd(<-- 描述特定 xml 文档结构的声明)生成类(完成了已经提到的注释)。
So you can choose what fits your needs best. Defining classes on your own and use XStream or use JAXB (or other binding frameworks...) and the bundled tools to generate classes. Each variant has advantages and disadvantages... XStream is more flexible but JAXB more strict. JAXB brings additional tools, XStream is more like a tool itself...
因此,您可以选择最适合您需求的产品。自己定义类并使用 XStream 或使用 JAXB(或其他绑定框架...)和捆绑工具来生成类。每个变体都有优点和缺点…… XStream 更灵活,但 JAXB 更严格。JAXB 带来了额外的工具,XStream 本身更像是一个工具......
回答by aymeric
I think it's not possible using XStream but I've already done it using JAXB.
我认为使用 XStream 是不可能的,但我已经使用 JAXB 完成了。
Basically, the steps are generating a xsd
from the xml files and then generating the Java
classes from the xsd.
基本上,这些步骤是xsd
从 xml 文件生成一个,然后Java
从 xsd生成类。
Have a look at this tutorial:
看看这个教程: