使用 XSD 以编程方式创建 XML 文档

发布于 2024-08-07 00:49:24 字数 293 浏览 2 评论 0原文

我需要使用符合一组 XSD 文件的内置 java org.w3c.dom 或 jdom 包创建 XML 文档。 XSD 文件相似但不相同。本质上,我需要根据动态提供给我的 XSD 创建 XML 文件。

最好的方法是什么。我已经检查了 Validator 包,它没有任何功能,例如针对给定上下文节点处可能元素的前瞻或迭代器。

理想情况下,我正在考虑一种 SAX 类型的架构,当我被要求以上下文无关的方式提供时,我会在其中实现一个元素。引擎应该组装起来并在最后给我 dom 树。

这个轮子已经发明了吗?

谢谢..

I need to create XML documents using built-in java org.w3c.dom or jdom packages conforming to a set of XSD files. The XSD files are similar but not the same. In essence, I need to create the XML file as per the XSD given to me dynamically.

What is the best way to do it. I have checked the Validator package, it does not have any features like look-ahead or iterators for the possible elements at a given context node.

Ideally, I am looking at a SAX kind of architecture where I implement an element when I am asked to provide in a context-free manner. The engine should assemble and give me the dom tree at the end.

Is this wheel already been invented?

Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

橪书 2024-08-14 00:49:24

org.w3c.dom 不支持 XML 模式。对于 JDom,您可以使用架构来验证文档,但不支持构建文档。

您有两个选择:

  1. 使用 JDom 读取模式,分析它并根据此 XML 文档生成文档(模式也始终是有效的 XML 文档)。

  2. 您可以使用 XML 映射器,例如 Castor。 Castor 可以读取架构并生成一组 Java 类,这些类允许您生成与所述架构匹配的文档。

后一种方法可能不太适合您的情况,因为 Castor 将为类似的 XSD 结构生成不同的 Java 类,除非它们是在同一文档中定义的,因此您无法轻松地重用公共代码。解决方法是让 Castor 生成代码,然后编写一个脚本或类似脚本来将 XSD 的公共部分复制/合并到同一个 Java 包中。

org.w3c.dom has no support for XML Schemas. For JDom, you can use schemas to validate a document, but there is no support to build documents.

You have two options:

  1. Use JDom to read the schema, analyze it and produce a document according to this XML document (a schema is always a valid XML document, too).

  2. You can use an XML mapper like Castor. Castor can read a Schema and produce a set of Java classes that allow you to produce documents which match said schema.

The latter approach might not work so well in your case since Castor will generate different Java classes for similar XSD structures unless they are defined in the same document, so you can't easily reuse the common code. The workaround is to let Castor generate the code and then write a script or similar to copy/merge the common parts of the XSDs into the same Java package.

扶醉桌前 2024-08-14 00:49:24

我目前正在解析 XSD,并且不是根据其中的某些部分创建 XML 文档,而是创建其他内容。我发现 XSOM 非常有用。看看 这个< /a> 简短的小教程,因为如何立即使用它并不明显。

一旦掌握了它的窍门,该库本身并不难使用,并且您可以获得 XSD 中找到的所有信息,即属性、限制和元素,您可以使用它们来生成 XML 文件。

I am currently parsing an XSD and creating not an XML document off of parts of it, but something else. I found that XSOM was very useful. Have a look at this short little tutorial as it isn't obvious how to use it right off.

The library itself isn't hard to use once you get the hang of it and you can get any all information found in the XSD i.e. attributes, restrictions and elements which you can use to generate a XML file off of.

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