如何从 DTD 或 XSD 生成示例 XML 文档?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17106/
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
How to generate sample XML documents from their DTD or XSD?
提问by lindelof
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?
我们正在开发一个涉及大量 XML 转换的应用程序。我们本身没有任何适当的输入测试数据,只有 DTD 或 XSD 文件。我们想从这些文件中自己生成我们的测试数据。有没有简单/免费的方法来做到这一点?
Edit
编辑
There are apparently no free tools for this, and I agree that OxygenXML is one of the best tools for this.
显然没有为此提供免费工具,我同意 OxygenXML 是最好的工具之一。
采纳答案by levand
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.
我认为 Oxygen ( http://www.oxygenxml.com/) 也是如此,但那是另一种商业产品。不过,这是一个不错的方法……我强烈建议任何从事大量 XML 工作的人使用它。它也带有一个不错的 Eclipse 插件。
I do believe there is a free, fully-featured 30 day trial.
我相信有一个免费的、功能齐全的 30 天试用版。
回答by Sam Warwick
In Visual Studio 2008 SP1 and later the XML Schema Explorercan create an XML document with some basic sample data:
在 Visual Studio 2008 SP1 和更高版本中,XML Schema Explorer可以使用一些基本示例数据创建 XML 文档:
- Open your XSD document
- Switch to XML Schema Explorer
- Right click the root node and choose "Generate Sample Xml"
- 打开您的 XSD 文档
- 切换到 XML 架构资源管理器
- 右键单击根节点并选择“生成示例 Xml”


回答by Florian Fankhauser
In recent versions of the free and open source EclipseIDE 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.
在免费和开源EclipseIDE 的最新版本中,您可以从 DTD 和 XSD 文件生成 XML 文档。右键单击给定的 *.dtd 或 *.xsd 文件,然后选择“生成 -> XML 文件...”。您可以选择要生成的根元素以及是否应生成可选属性和元素。
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.
当然,您也可以使用 Eclipse 创建和编辑 DTD 和 XSD 模式文件。而且您不需要安装任何插件。它包含在标准发行版中。
回答by yamass
For Intellij Idea users:
对于 Intellij Idea 用户:
Have a look at Tools -> XML Actions
看看工具 -> XML 操作


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..."
正如@naXa 所提到的,您现在还可以右键单击 XSD 文件,然后单击“从 XSD 架构生成 XML 文档...”
回答by levand
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.
Sourceforge.net 上可用的 camprocessor 将为任何 XSD 生成 xml 测试用例。有一个教程可以向您展示如何生成自己的测试示例 - 包括使用内容提示来确保真实的示例,而不仅仅是随机的垃圾示例。
The tutorial is available here: http://www.oasis-open.org/committees/download.php/29661/XSD%20and%20jCAM%20tutorial.pdf
该教程可在此处获得: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
有关该工具的更多信息 - 该工具使用 OASIS 内容组装机制 (CAM) 标准将 XSD 重构为对 XSLT 更友好的结构 - 可以从资源网站 - http://www.jcam.org.uk 找到
Enjoy, DW
享受,DW
回答by falko
You can use the XML Instance Generator which is part of the Sun/Oracle Multi-Schema Validator.
您可以使用 XML Instance Generator,它是Sun/Oracle Multi-Schema Validator 的一部分。
It's README.txt states:
它的 README.txt 声明:
Sun XML Generator is a Java tool to generate various XML instances from several kinds of schemas. It supports DTD, RELAX Namespace, RELAX Core, TREX, and a subset of W3C XML Schema Part 1. [...]
This is a command-line tool that can generate both valid and invalid instances from schemas. It can be used for generating test cases for XML applications that need to conform to a particular schema.
Sun XML Generator 是一个 Java 工具,用于从多种模式生成各种 XML 实例。它支持 DTD、RELAX Namespace、RELAX Core、TREX 和 W3C XML Schema Part 1 的一个子集。 [...]
这是一个命令行工具,可以从模式生成有效和无效的实例。它可用于为需要符合特定模式的 XML 应用程序生成测试用例。
Download and unpack xmlgen.zipfrom the msv download pageand run the following command to get detailed usage instructions:
xmlgen.zip从msv下载页面下载解压,运行以下命令获取详细使用说明:
java -jar xmlgen.jar -help
java -jar xmlgen.jar -help
The tool appears to be released under a BSD license; the source code is accessible from here
该工具似乎是在 BSD 许可下发布的;源代码可以从这里访问
回答by SCdF
XMLSpydoes that for you, although that's not free...
I believe that Liquid Xml Studiodoes it for you and is free, but I have not personally used it to create test data.
我相信Liquid Xml Studio会为您完成并且是免费的,但我个人没有使用它来创建测试数据。
回答by Michal Rames
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:
我使用 EclipseLink 的 MOXy 动态生成绑定类,然后递归遍历绑定类型。它有点重,但是一旦对象树被实例化,它就允许 XPath 值注入:
InputStream in = new FileInputStream(PATH_TO_XSD);
DynamicJAXBContext jaxbContext =
DynamicJAXBContextFactory.createContextFromXSD(in, null, Thread.currentThread().getContextClassLoader(), null);
DynamicType rootType = jaxbContext.getDynamicType(YOUR_ROOT_TYPE);
DynamicEntity root = rootType.newDynamicEntity();
traverseProps(jaxbContext, root, rootType, 0);
TraverseProps is pretty simple recursive method:
TraverseProps 是一个非常简单的递归方法:
private void traverseProps(DynamicJAXBContext c, DynamicEntity e, DynamicType t, int level) throws DynamicException, InstantiationException, IllegalAccessException{
if (t!=null) {
logger.info(indent(level) + "type [" + t.getName() + "] of class [" + t.getClassName() + "] has " + t.getNumberOfProperties() + " props");
for (String pName:t.getPropertiesNames()){
Class<?> clazz = t.getPropertyType(pName);
logger.info(indent(level) + "prop [" + pName + "] in type: " + clazz);
//logger.info("prop [" + pName + "] in entity: " + e.get(pName));
if (clazz==null){
// need to create an instance of object
String updatedClassName = pName.substring(0, 1).toUpperCase() + pName.substring(1);
logger.info(indent(level) + "Creating new type instance for " + pName + " using following class name: " + updatedClassName );
DynamicType child = c.getDynamicType("generated." + updatedClassName);
DynamicEntity childEntity = child.newDynamicEntity();
e.set(pName, childEntity);
traverseProps(c, childEntity, child, level+1);
} else {
// just set empty value
e.set(pName, clazz.newInstance());
}
}
} else {
logger.warn("type is null");
}
}
Converting everything to XML is pretty easy:
将所有内容转换为 XML 非常简单:
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
回答by Pat B
You can also use XMLPad (free to use) found here http://www.wmhelp.comto generate your xml samples. From the menu : XSD -> generate sample XML file.
您还可以使用XMLPad(免费使用),在这里找到http://www.wmhelp.com生成你的XML样本。从菜单:XSD -> 生成示例 XML 文件。
回答by rolandpeng
XML-XIG: XML Instance Generator
XML-XIG:XML 实例生成器
http://xml-xig.sourceforge.net/
http://xml-xig.sourceforge.net/
This opensource would be helpful.
这个开源会很有帮助。

