Java 从 XML 模式 (xsd) 生成 XML 实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/307616/
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
XML instance generation from XML schema (xsd)
提问by
I was wondering if there's a way I can automate the generation of XML files from XSD schemas given that I have the data and the labels. I'd like to do this in python/java. It seems very possible, yet I can't find any library that allows me to do this. I'm looking for a fairly quick solution.. Any ideas?
我想知道是否有一种方法可以在我拥有数据和标签的情况下从 XSD 模式自动生成 XML 文件。我想在 python/java 中执行此操作。这似乎很有可能,但我找不到任何允许我这样做的库。我正在寻找一个相当快速的解决方案..有什么想法吗?
See also: how-to-generate-sample-xml-documents-from-their-dtd-or-xsd
另请参阅:how-to-generate-sample-xml-documents-from-their-dtd-or-xsd
回答by Kieveli
JAXBworks fantastic for generating classes from xsd.
JAXB非常适合从 xsd 生成类。
Ibatisworks fantastic for getting data into classes.
Ibatis非常适合将数据放入类中。
You can use Ibatis to feed data and automatically create classes, then use JAXB to marshal the classes into an XML file! Mind you, that's a lot of effort if you're not going to be doing it over and over again.
您可以使用 Ibatis 提供数据并自动创建类,然后使用 JAXB 将类编组为 XML 文件!请注意,如果您不打算一遍又一遍地这样做,那需要付出很多努力。
回答by S.Lott
回答by Luis Soeiro
I recommend two approaches:
我推荐两种方法:
Xstream- it let's you generate XML files by defining a Java file and either putting Java annotations on the items or just defining aliases. It is very easy, but it is not fully automatic;
XMLBeans- these tools let's you generate Java files from XML schema definitions (xsd) so that you can import, manipulate, create and export XML files using JavaBeans-like method calls.
Xstream- 它让您通过定义 Java 文件并在项目上放置 Java 注释或仅定义别名来生成 XML 文件。这很容易,但不是全自动的;
XMLBeans- 这些工具让您可以从 XML 模式定义 (xsd) 生成 Java 文件,以便您可以使用类似 JavaBeans 的方法调用导入、操作、创建和导出 XML 文件。
Regards, Luis
问候, 路易斯
回答by Cheeso
Microsoft has published a "document generator" tool as a sample. This is an articlethat describes the architecture and operation of the sample app in some detail.
Microsoft 发布了一个“文档生成器”工具作为示例。 这篇文章比较详细地描述了示例应用程序的架构和操作。
If you just want to use the document generation tool, click hereand install the MSI. It requires no programming.
如果您只想使用文档生成工具,请单击此处并安装 MSI。它不需要编程。
It's free. The source is available. Requires the .NET Framework to run. Works only with XSDs. (not Relax NG or DTD).
免费。来源是可用的。需要 .NET Framework 才能运行。仅适用于 XSD。(不是放松 NG 或 DTD)。
回答by Symen Timmermans
I use Exchanger XML Editor for this purpose. You can download it for free for multiple operating systems at: http://www.exchangerxml.com/
为此,我使用 Exchanger XML 编辑器。您可以在以下网址免费下载适用于多种操作系统的软件:http: //www.exchangerxml.com/
The option is in menu "Schema" -> "Schema instance generation".
该选项位于菜单“架构”->“架构实例生成”中。