将巨大的模式编译成 Java [英] Compiling huge schema into Java

查看:32
本文介绍了将巨大的模式编译成 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种主要工具提供了将 XSD 模式编译为 Java 的方法:xmlbeans 和 JAXB.

There are two major tools which provides a way to compile XSD schema into Java: xmlbeans and JAXB.

问题是 XSD 模式真的很大:30MB 的 XML 文件,大部分模式没有在我的项目中使用,所以我可以注释掉大部分代码,但这不是一个好的解决方案.

The problem is the XSD schema is really huge: 30MB of XML files, most of the schema isn't used in my project, so I can comment out most of the code, but it not a good solution.

目前我的项目使用 xmlbeans,它编译具有重大变化的架构.它生成约 60MB 的类,编译需要约 30 分钟.

Currently my project uses xmlbeans which compiles the schema with major changes. It produces ~60MB of classes and it takes ~30 min to compile.

另一种解决方案是使用 JAXB,它无需编辑代码即可生成约 14MB 的代码.但它产生了巨大的 ObjectFactory 类,该类无法编译,并出现常量过多"错误.我可以扔掉这个类并在没有它的情况下编译架构,但据我所知,它是非常有用的类.

Another solution is to use JAXB, which generates ~14MB of code without need to edit the code. But it produces huge ObjectFactory class, which fails to compile with "too many constants" error. I can throw the class away and compile the schema without it, but as I understand, it's very useful class.

任何想法如何处理这个庞大的架构?

Any ideas how to handle this huge schema?

推荐答案

在使用 XmlBeans 或 JAXB 映射之前,您能否创建一个脚本来提取您需要的架构部分并将其集成到您的构建过程中?

Could you create a script to extract the portion(s) of the schema you need and integrate that into your build process prior to mapping with XmlBeans or JAXB?

您可能可以在 Python、Perl、Awk 等中简单轻松地编写此提取的脚本;或者甚至在 XSL 方面,如果您有这方面的专业知识(我从来没有花足够的时间编写 XSL 来精通 XSL,所以我可能会坚持使用脚本语言,但这只是我自己).

You could probably script this extraction fairly simply and easily in Python, Perl, Awk, etc; or even in XSL if you have expertise there (I've never spent enough contiguous time coding XSL to get proficient, so I'd probably stick to a scripting language, but that's just me).

例如:

python extract.py big-schema.xsd >small-schema.xsd
xsd2java <args> small-schema.xsd
...

您可能会发现第 3 方供应商的后续更新会使您的提取脚本无效,但除非他们对整体架构进行非常大的更改,否则您应该能够相当快地更新脚本,而且听起来就像这些更新应该很少发生一样.

You might find that a subsequent update by the 3rd-party vendor would invalidate your extraction script, but unless they're making very large changes to the overall schema, you should be able to update the script fairly quickly, and it sounds like those updates should be fairly infrequent.

顺便说一句,我有点偏爱 XmlBeans;当我们对 XML-Java 映射工具进行自己的评估时,它似乎比我们尝试过的其他任何东西都能更好地处理像 xs:choice、xs:all 和类型替换这样的结构.但那是几年前的事了,现在肯定已经改变了.在这一点上,我们继续使用它更多是出于制度惯性而不是其他任何事情,因此请谨慎对待该建议.

Incidentally, I'm a little partial to XmlBeans; when we did our own evaluation of XML-Java mapping tools, it seemed to handle constructs like xs:choice, xs:all, and type-substitution better than anything else we tried. But that was several years ago, and could certainly have changed by now. At this point, we're continuing to use it more out of institutional inertia than anything else, so take that recommendation with a dash of salt.

这篇关于将巨大的模式编译成 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆