如何从 DTD 或 XSD 生成示例 XML 文档?

发布于 2024-10-29 03:57:06 字数 184 浏览 2 评论 0原文

我们正在开发一个涉及大量 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

ま柒月 2024-11-05 03:57:06

在 Visual Studio 2008 SP1 及更高版本中,XML 架构资源管理器 可以创建 XML包含一些基本示例数据的文档:

  1. 打开 XSD 文档
  2. 切换到 XML 模式资源管理器
  3. 右键单击​​根节点并选择“生成示例 Xml”

 XML 模式资源管理器的屏幕截图

In Visual Studio 2008 SP1 and later the XML Schema Explorer can create an XML document with some basic sample data:

  1. Open your XSD document
  2. Switch to XML Schema Explorer
  3. Right click the root node and choose "Generate Sample Xml"

Screenshot of the XML Schema Explorer

霊感 2024-11-05 03:57:06

在最新版本的免费开源 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.

哎呦我呸! 2024-11-05 03:57:06

对于 Intellij Idea 用户:

查看工具 -> XML 操作

在此处输入图像描述

似乎工作得很好(据我测试)。

编辑:

正如 @naXa 所提到的,您现在还可以右键单击 XSD 文件,然后单击“从 XSD 架构生成 XML 文档...”

For Intellij Idea users:

Have a look at Tools -> XML Actions

enter image description here

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..."

渡你暖光 2024-11-05 03:57:06

我认为 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.

短暂陪伴 2024-11-05 03:57:06

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

回忆躺在深渊里 2024-11-05 03:57:06

您可以使用 XML 实例生成器,它是 Sun/Oracle Multi-Schema Validator 的一部分。

它的 README.txt 指出:

Sun XML Generator 是一个 Java 工具,用于生成各种 XML 实例
几种模式。它支持 DTD、RELAX 命名空间、RELAX 核心、
TREX,以及 W3C XML 架构第 1 部分的子集。[...]

这是一个命令行工具,可以生成有效和无效的
来自模式的实例。它可用于生成 XML 测试用例
需要符合特定架构的应用程序。

msv 下载页面 下载并解压 xmlgen.zip 并运行以下命令命令获取详细使用说明:

java -jar xmlgen.jar -help

该工具似乎是在 BSD 许可证下发布的;源代码可从此处访问

You can use the XML Instance Generator which is part of the Sun/Oracle Multi-Schema Validator.

It's README.txt states:

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.

Download and unpack xmlgen.zip from the msv download page and run the following command to get detailed usage instructions:

java -jar xmlgen.jar -help

The tool appears to be released under a BSD license; the source code is accessible from here

夜空下最亮的亮点 2024-11-05 03:57:06

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.

不离久伴 2024-11-05 03:57:06

到目前为止似乎没有人能够回答这个问题:)

我使用 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 是非常简单的递归方法:

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");
        }
    }

将所有内容转换为 XML 非常容易:

Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);

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:

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:

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:

Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
べ繥欢鉨o。 2024-11-05 03:57:06

您还可以使用此处的 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.

痞味浪人 2024-11-05 03:57:06

XML-XIG:XML 实例生成器

http://xml-xig.sourceforge.net/

这个开源将乐于助人。

XML-XIG: XML Instance Generator

http://xml-xig.sourceforge.net/

This opensource would be helpful.

看春风乍起 2024-11-05 03:57:06

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.

转角预定愛 2024-11-05 03:57:06

微软已经发布了一个“文档生成器”工具作为示例。 本文介绍了示例应用的体系结构和操作在一些细节上。

如果您只想运行示例生成工具,单击此处并安装 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).

南巷近海 2024-11-05 03:57:06

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

黯然#的苍凉 2024-11-05 03:57:06

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.

她如夕阳 2024-11-05 03:57:06

Liquid XML Studio 有一个 XML 示例生成器 向导,它将从中构建示例 XML 文件XML 模式。生成的数据似乎符合架构(它只是无法生成正则表达式模式的数据)。

从 XSD 生成 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).

Generate an XML Sample from an XSD

情话已封尘 2024-11-05 03:57:06

OpenXSD 库提到它们支持基于 XSD 生成 XML 实例。检查一下。

The OpenXSD library mentions that they have support for generating XML instances based on the XSD. Check that out.

旧情别恋 2024-11-05 03:57:06

为了完整起见,我将添加 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?

风筝有风,海豚有海 2024-11-05 03:57:06

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.

独享拥抱 2024-11-05 03:57:06

还有 http://xsd2xml.com/,一个在线 XSD 到 XML 生成器

There's also http://xsd2xml.com/, an online XSD to XML generator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文