如何从 DTD 或 XSD 生成示例 XML 文档?
我们正在开发一个涉及大量 XML 转换的应用程序。我们本身没有任何正确的输入测试数据,只有 DTD 或 XSD 文件。我们想从这些文件中生成我们的测试数据。有没有一种简单/免费的方法可以做到这一点?
编辑
显然没有免费的工具可以做到这一点,我同意 OxygenXML 是最好的工具之一。
We are developing an application that involves a substantial amount of XML transformations. We do not have any proper input test data per se, only DTD or XSD files. We'd like to generate our test data ourselves from these files. Is there an easy/free way to do that?
Edit
There are apparently no free tools for this, and I agree that OxygenXML is one of the best tools for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
在 Visual Studio 2008 SP1 及更高版本中,XML 架构资源管理器 可以创建 XML包含一些基本示例数据的文档:
In Visual Studio 2008 SP1 and later the XML Schema Explorer can create an XML document with some basic sample data:
在最新版本的免费开源 Eclipse IDE 中,您可以从 DTD 和 XSD 文件生成 XML 文档。右键单击给定的 *.dtd 或 *.xsd 文件,然后选择“生成 -> XML 文件...”。您可以选择要生成哪个根元素以及是否应生成可选属性和元素。
当然,您也可以使用 Eclipse 创建和编辑 DTD 和 XSD 模式文件。而且您不需要安装任何插件。它包含在标准发行版中。
In recent versions of the free and open source Eclipse IDE you can generate XML documents from DTD and XSD files. Right-click on a given *.dtd or *.xsd file and select "Generate -> XML File...". You can choose which root element to generate and whether optional attributes and elements should be generated.
Of course you can use Eclipse to create and edit your DTD and XSD schema files, too. And you don't need to install any plugins. It is included in the standard distribution.
对于 Intellij Idea 用户:
查看工具 -> XML 操作
似乎工作得很好(据我测试)。
编辑:
正如 @naXa 所提到的,您现在还可以右键单击 XSD 文件,然后单击“从 XSD 架构生成 XML 文档...”
For Intellij Idea users:
Have a look at Tools -> XML Actions
Seems to work very well (as far as I have tested).
Edit:
As mentioned by @naXa, you can now also right-click on the XSD file and click "Generate XML Document from XSD Schema..."
我认为 Oxygen (http://www.oxygenxml.com/) 也能做到这一点,但那是另一个商业广告产品。不过,这是一个很好的工具...我强烈推荐给任何从事大量 XML 工作的人。它也有一个很好的 Eclipse 插件。
我确实相信有一个免费的、功能齐全的 30 天试用版。
I think Oxygen (http://www.oxygenxml.com/) does it as well, but that's another commerical product. It's a nice one, though... I'd strongly recommend it for anyone doing a lot of XML work. It comes in a nice Eclipse plugin, too.
I do believe there is a free, fully-featured 30 day trial.
Sourceforge.net 上提供的 camprocessor 将为任何 XSD 生成 xml 测试用例。有一个教程可向您展示如何生成自己的测试示例 - 包括使用内容提示来确保示例真实,而不仅仅是随机的垃圾示例。
该教程可在此处获取:
http://www.oasis-open .org/committees/download.php/29661/XSD%20and%20jCAM%20tutorial.pdf
有关该工具的更多信息 - 该工具使用 OASIS 内容组装机制 (CAM) 标准将您的 XSD 重构为更XSLT 友好结构 - 可以从资源网站找到 - http://www.jcam.org.uk
享受,DW
The camprocessor available on Sourceforge.net will do xml test case generation for any XSD. There is a tutorial available to show you how to generate your own test examples - including using content hints to ensure realistic examples, not just random junk ones.
The tutorial is available here:
http://www.oasis-open.org/committees/download.php/29661/XSD%20and%20jCAM%20tutorial.pdf
And more information on the tool - which is using the OASIS Content Assembly Mechanism (CAM) standard to refactor your XSD into a more XSLT friendly structure - can be found from the resource website - http://www.jcam.org.uk
Enjoy, DW
您可以使用 XML 实例生成器,它是 Sun/Oracle Multi-Schema Validator 的一部分。
它的 README.txt 指出:
从 msv 下载页面 下载并解压
xmlgen.zip
并运行以下命令命令获取详细使用说明:该工具似乎是在 BSD 许可证下发布的;源代码可从此处访问
You can use the XML Instance Generator which is part of the Sun/Oracle Multi-Schema Validator.
It's README.txt states:
Download and unpack
xmlgen.zip
from the msv download page and run the following command to get detailed usage instructions:The tool appears to be released under a BSD license; the source code is accessible from here
XMLSpy 可以为您做到这一点,尽管这不是免费的......
我相信 Liquid Xml Studio 可以为您做到这一点并且是免费的,但我个人还没有使用它来创建测试数据。
XMLSpy does that for you, although that's not free...
I believe that Liquid Xml Studio does it for you and is free, but I have not personally used it to create test data.
到目前为止似乎没有人能够回答这个问题:)
我使用 EclipseLink 的 MOXy 动态生成绑定类,然后递归地遍历绑定类型。它有点重,但一旦实例化对象树,它就允许 XPath 值注入:
TraverseProps 是非常简单的递归方法:
将所有内容转换为 XML 非常容易:
Seems like nobody was able to answer the question so far :)
I use EclipseLink's MOXy to dynamically generate binding classes and then recursively go through the bound types. It is somewhat heavy, but it allows XPath value injection once the object tree is instantiated:
TraverseProps is pretty simple recursive method:
Converting everything to XML is pretty easy:
您还可以使用此处的 XMLPad(免费使用)http://www.wmhelp.com
生成您的 xml 样本。
从菜单:XSD ->生成示例 XML 文件。
You can also use XMLPad (free to use) found here http://www.wmhelp.com
to generate your xml samples.
From the menu : XSD -> generate sample XML file.
XML-XIG:XML 实例生成器
http://xml-xig.sourceforge.net/
这个开源将乐于助人。
XML-XIG: XML Instance Generator
http://xml-xig.sourceforge.net/
This opensource would be helpful.
Microsoft Office 有“InfoPath”,它采用 XSD 作为导入并让您可以快速轻松地定义基于表单的编辑器来创建 XML 文件。它有两种模式 - 一种模式是您定义表单,另一种模式是您通过填写表单来创建 XML 文件。我相信它首先随 Office 2003 一起提供,但大多数人从未安装过它。我对它的喜爱程度感到震惊。
Microsoft Office has 'InfoPath', which takes an XSD as an import and lets you quickly and easily define a form-based editor for creating XML files. It has two modes - one where you define the form, and another mode where you create the XML file by filling out the form. I believe it first came with Office 2003, and most people never install it. It shocks me at how much I like it.
微软已经发布了一个“文档生成器”工具作为示例。 本文介绍了示例应用的体系结构和操作在一些细节上。
如果您只想运行示例生成工具,单击此处并安装 MSI。
它是免费的。来源可用。需要 .NET Framework 才能运行。仅适用于 XSD。 (不是 Relax NG 或 DTD)。
Microsoft has published a "document generator" tool as a sample. This is an article that describes the architecture and operation of the sample app in some detail.
If you just want to run the sample generation tool, click here and install the MSI.
It's free. The source is available. Requires the .NET Framework to run. Works only with XSDs. (not Relax NG or DTD).
XMLBlueprint 7.5 可以执行以下操作:
- 从 dtd 生成示例 xml
- 从relax ng模式生成示例xml
- 从 xml 模式生成示例 xml
XMLBlueprint 7.5 can do the following:
- generate sample xml from dtd
- generate sample xml from relax ng schema
- generate sample xml from xml schema
SoapUI 的开源版本可以从 WSDL(包含 XSD 类型定义)生成 SOAP 请求,因此看起来像有一个此功能的开源实现。不幸的是,我还没有弄清楚哪个库是用来做这个的。
The open source Version of SoapUI can generate SOAP requests from WSDL (which contains XSD type definitions), so it looks like there IS an open source implementation of this functionality. Unfortunately, I haven't figured out which library is used to to this.
Liquid XML Studio 有一个 XML 示例生成器 向导,它将从中构建示例 XML 文件XML 模式。生成的数据似乎符合架构(它只是无法生成正则表达式模式的数据)。
Liquid XML Studio has an XML Sample Generator wizard which will build sample XML files from an XML Schema. The resulting data seems to comply with the schema (it just can't generate data for regex patterns).
OpenXSD 库提到它们支持基于 XSD 生成 XML 实例。检查一下。
The OpenXSD library mentions that they have support for generating XML instances based on the XSD. Check that out.
为了完整起见,我将添加 http://code.google.com/p/jlibs/ wiki/XSInstance,在类似的(但特定于 Java 的)问题中提到过:用于生成示例的任何 Java“API”来自 XSD 的 XML?
For completeness I'll add http://code.google.com/p/jlibs/wiki/XSInstance, which was mentioned in a similar (but Java-specific) question: Any Java "API" to generate Sample XML from XSD?
XML 蓝图也能做到这一点;此处的说明
http://www.xmlblueprint.com/help/html/topic_170.htm
它不是免费的,但有 10 天的免费试用期;看起来又快又高效;不幸的是它仅限于Windows。
XML Blueprint also does that; instructions here
http://www.xmlblueprint.com/help/html/topic_170.htm
It's not free, but there's a 10-day free trial; it seems fast and efficient; unfortunately it's Windows only.
还有 http://xsd2xml.com/,一个在线 XSD 到 XML 生成器
There's also http://xsd2xml.com/, an online XSD to XML generator