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

时间:2023-01-12
本文介绍了任何 Java“API"从 XSD 生成示例 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 的工具进行的.找不到任何对 API 的引用.
how-to-generate-sample-xml-来自他们的 dtd 或 xsd 的文档
tool-to-generate-xml-file-from-xsd-用于测试
xml-instance-generation-from-xml-schema-xsd

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

推荐答案

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

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:integer nodes, etc, etc..

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

Once it's done, the rest is not a problem: traversing XSD with XPath or 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.

这篇关于任何 Java“API"从 XSD 生成示例 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:JAXB @XmlElements minOccurs = 1 下一篇:JAXB 2.1 - 自定义 xs:any 绑定

相关文章

最新文章