問題描述
作為我們應(yīng)用程序的一部分,我們需要開發(fā)一個接收 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?
搜索了這個論壇,發(fā)現(xiàn)了以下類似的問題.但討論是圍繞從 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 節(jié)點"/>
,處理 minoccurs/maxoccurs 屬性,不在 xs:integer
節(jié)點等中存儲日期.
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..
一旦完成,剩下的就不是問題了:使用 XPath
或 org.w3c.dom.*
遍歷 XSD,扁平化復(fù)雜類型和擴展就可以解決問題.我敢打賭,在發(fā)布問題后的幾分鐘內(nèi),您會在 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.
這篇關(guān)于任何 Java“API"從 XSD 生成示例 XML?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!