Java 到 XSD 或 XSD 到 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12943011/
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
Java to XSD or XSD to Java
提问by Paul Reiners
I know that, using JAXB, you can generate Java files from an XSDand that you can also generate the XSD from annotated POJOs. What are the advantages and disadvantages of each? Is one overall better than the other?
我知道,使用 JAXB,您可以从 XSD 生成 Java 文件,也可以从带注释的 POJO 生成 XSD。各自的优缺点是什么?一个整体比另一个好?
We basically want to serialize events to a log in XML format.
我们基本上想将事件序列化为 XML 格式的日志。
采纳答案by bdoughan
Ultimately it depends on where you want to focus:
归根结底,这取决于您要关注的重点:
If the XML Schema is the Most Important Thing
如果 XML Schema 是最重要的事情
Then it is best to start from the XML schema and generate a JAXB model. There are details of an XML schema that a JAXB (JSR-222) implementation just can't generate:
那么最好从 XML 模式开始,生成 JAXB 模型。有 JAXB (JSR-222) 实现无法生成的 XML 模式的详细信息:
- A maxOccurs other than 0, 1, or unbounded
- many facets on a simple type
- model groups
- A maxOccurs 不是 0、1 或无界
- 一个简单类型的许多方面
- 模型组
If the Object Model is the Most Important Thing
如果对象模型是最重要的
If you will be using the Java model for more than just converting between objects and XML (i.e. using it with JPA for persistence) then I would recommend starting with Java objects. This will give you the greatest control.
如果您将使用 Java 模型不仅仅是在对象和 XML 之间进行转换(即,将它与 JPA 一起使用以实现持久性),那么我建议从 Java 对象开始。这会给你最大的控制。
回答by Yogendra Singh
It depends on your requirement and scenario with respect to the point of initiation.
这取决于您对启动点的要求和场景。
Given your requirement, use generate Java files from an XSD
as you want to define the output(XML) format first which should be supported by Java.,
根据您的要求,使用generate Java files from an XSD
您想首先定义Java应该支持的输出(XML)格式。,
回答by DNA
Given that one of the main points of XML is to be a portable data-transfer format, usable regardless of platform or language, I would avoid generating XSD from any specific programming language, as a rule of thumb. This may not matter if you know you are only communicating between Java endpoints (but are you sure this will be true forever?).
鉴于 XML 的一个要点是成为一种可移植的数据传输格式,无论平台或语言如何都可用,我会避免从任何特定的编程语言生成 XSD,作为经验法则。如果您知道您只是在 Java 端点之间进行通信,这可能无关紧要(但您确定这将永远正确吗?)。
It is better, all else being equal, to define the interface/schema in a programming-language neutral way.
在其他条件相同的情况下,最好以编程语言中立的方式定义接口/模式。
There are lots of exceptions to this general principle, especially if you are integrating with existing or legacy code...
这个一般原则有很多例外,特别是如果你正在与现有或遗留代码集成......
回答by Petter Nordlander
If you have the opportunity to design both pojo and schema, It's a matter of design - do you design for a "perfect" schema or for a "perfect" java class.
如果您有机会设计 pojo 和模式,这是一个设计问题 - 您是为“完美”模式还是为“完美”java 类设计。
In some cases, you don't have the luxury of a choice, in system integration scenarios, you might be given a predefined XSD from another system that you need to adapt to, then XSD -> Class will be the only way.
在某些情况下,您没有选择的余地,在系统集成场景中,您可能会从另一个需要适应的系统中获得预定义的 XSD,那么 XSD -> Class 将是唯一的方法。