从 XSD 生成示例 XML 的任何 Java“API”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4710852/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:39:12  来源:igfitidea点击:

Any Java "API" to generate Sample XML from XSD?

javaxmlxsd

提问by Riyaz

As part of our application we need to develop a module that takes in an XSD schema and gives out a sample XML. The XSD schemas will be supplied during runtime. So is there any Java API out there that can do the job?

作为我们应用程序的一部分,我们需要开发一个模块,该模块接收 XSD 模式并提供示例 XML。XSD 模式将在运行时提供。那么是否有任何 Java API 可以完成这项工作?

Searched this forum and found the following similar questions. But the discussions were around tools to generate sample XML from XSD. Could not find any reference to APIs.
how-to-generate-sample-xml-documents-from-their-dtd-or-xsd
tool-to-generate-xml-file-from-xsd-for-testing
xml-instance-generation-from-xml-schema-xsd

搜索了这个论坛,发现了以下类似的问题。但讨论是围绕从 XSD 生成示例 XML 的工具展开的。找不到任何对 API 的引用。
How-to-generate-sample-xml-documents-from-their-dtd-or-xsd
tool-to-generate-xml-file-from-xsd-for-testing
xml-instance-generation-from-xml-schema- xsd

采纳答案by andbi

There is no such API, but it's possible. 'gives out sample XML' means that you will have to implement generation of sample XML node(s) from XSD basic types like <xs:element name="value" type="xs:integer" minOccurs="0"/>, taking care of minoccurs/maxoccurs attributes, not storing dates in xs:integernodes, etc, etc..

没有这样的 API,但它是可能的。“给出示例 XML”意味着您必须从 XSD 基本类型实现示例 XML 节点的生成,例如<xs:element name="value" type="xs:integer" minOccurs="0"/>处理 minoccurs/maxoccurs 属性,不在xs:integer节点中存储日期等。

Once it's done, the rest is not a problem: traversing XSD with XPathor org.w3c.dom.*, flattening complex types and extensions will do the trick. I bet you'll receive working traversing code here on stackoverflow within minutes after posting question.

一旦完成,剩下的就不是问题:用XPath或遍历XSD org.w3c.dom.*,扁平化复杂类型和扩展就行了。我敢打赌,您会在发布问题后的几分钟内在 stackoverflow 上收到有效的遍历代码。

回答by nir

i found java API to generate XML sample from XSD http://code.google.com/p/jlibs/wiki/XSInstance,

我找到了从 XSD http://code.google.com/p/jlibs/wiki/XSInstance生成 XML 示例的 Java API ,