是否有标准的 Java 实用程序可以在运行时解析 XSD 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1172101/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:29:56  来源:igfitidea点击:

Is there a standard Java utility to parse XSD files at runtime?

javaxmlxsd

提问by Andrew

I've been looking around for a way to programmatically parse XSD files to generate an XML mapping so that I can take any XSD file and create a parser that will parse any XML file that conforms to it, and my google-fu has been coming up short.

我一直在寻找一种以编程方式解析 XSD 文件以生成 XML 映射的方法,这样我就可以获取任何 XSD 文件并创建一个解析器来解析符合它的任何 XML 文件,我的 google-fu 来了短。

XSOMlooks promising, as it will parse an XSD file and make all its attributes available in a straight forward way.

XSOM看起来很有前途,因为它将解析 XSD 文件并以直接的方式使其所有属性可用。

Are there any other options or standard libraries available that will produce an XML file parser from an XSD file?

是否有任何其他选项或标准库可以从 XSD 文件生成 XML 文件解析器?

[Conclusion]Thanks everyone for your responses, they were a real help. I was able to write implementations using JAXP, Eclipses EMF and in XSOM that all worked as desired.

【结论】感谢大家的回复,真的很有帮助。我能够使用 JAXP、Eclipses EMF 和 XSOM 编写实现,所有这些都按预期工作。

JAXP was very straight forward and easy to learn and do. EMF was actually a pain to get going properly, there were so many jars that had to be included for it to work standalone that I would not recommend it. XSOM was even simpler than the JAXP implementation, so I went with it in the end.

JAXP 非常简单,易于学习和操作。EMF 实际上很难正常运行,必须包含很多 jar 才能使其独立工作,因此我不推荐它。XSOM 甚至比 JAXP 实现更简单,所以我最终选择了它。

回答by Michael van der Westhuizen

Yes, there's a standard way.

是的,有一个标准的方法。

You're looking for JAXB.

您正在寻找JAXB

回答by jsight

If you are looking at something for binding the XML to Java objects, then there are some choices:

如果您正在寻找将 XML 绑定到 Java 对象的方法,那么有一些选择:

回答by jsight

I've used the Eclipse XSD libraries to parse XML Schema in the past. http://eclipse.org/modeling/mdt/?project=xsd, and found it very straighforward to use There are a ton of jar files that come down, but you will only need 5 of them at runtime to do the parsing.

我过去曾使用 Eclipse XSD 库来解析 XML 模式。 http://eclipse.org/modeling/mdt/?project=xsd,并发现使用起来非常简单 有大量的 jar 文件,但在运行时您只需要其中的 5 个即可进行解析。

回答by Miguel Gamboa

xmlet/XsdParserparses a XSD file into a Java Structure:

xmlet/XsdParser将 XSD 文件解析为 Java 结构:

String filePath = "Your file path here.";
XsdParser parserInstance = new XsdParser();
Stream<XsdElement> elementsStream = parserInstance.parse(filePath);

Then you can navigate on the parsed elementsStreamsuch as:

然后您可以在解析后导航,elementsStream例如:

XsdElement htmlElement = elements.findFirst().get();

XsdComplexType htmlComplexType = htmlElement.getXsdComplexType();
XsdAttribute manifestAttribute = htmlComplexType.getXsdAttributes().findFirst().get();

XsdChoice choiceElement = (XsdChoice) htmlComplexType.getXsdChildElement();

XsdGroup flowContentGroup = (XsdGroup) choiceElement.getXsdElements().findFirst().get();

XsdAll flowContentAll = (XsdAll) flowContentGroup.getChildElement();

XsdElement elem1 = (XsdElement) flowContentAll.getXsdElements().findFirst().get();

This example navigates on types defined by an Html5 schema.

此示例导航由 Html5 架构定义的类型。

回答by Stroboskop

My personal favourite is Apache's XMLBeans.
It can create a Jar file containing parser, writer and verifier based on an XSD.

我个人最喜欢的是 Apache 的XMLBeans
它可以创建一个包含基于 XSD 的解析器、编写器和验证器的 Jar 文件。