开发者

Options for JAXB 2.1 Bindings Customization

开发者 https://www.devze.com 2022-12-21 04:39 出处:网络
I am working on generating Java objects from an XSD file using JAXB 2.1.The XSD file has several elements in it representing business model entities, with common names like Account, etc.The system whi

I am working on generating Java objects from an XSD file using JAXB 2.1. The XSD file has several elements in it representing business model entities, with common names like Account, etc. The system which is using the generated files to unmarshal the XML has several conflicting class names in its domain model. Though we can use different package names to get around class name conflicts, I think it will be more readable/maintainable to have objects of different names.

Because of this, I'd like to alter the XJC compilation so that is produces objects like: DataTransferAccount.java, etc. instead of Account.java. Super, I'll use one of the two options JAXB provides when binding a schema (http://java.sun.com/webservices/docs/2.0/tutorial/doc/JAX开发者_如何学CBUsing4.html):

  1. Inline Customizations - Annotate the XSD itself using the jaxb namespace to specify class names
  2. External Bindings File - Provide an extra file to the XJC which has rules on how to map schema elements to java classes

Is there a good argument for using option 1, aside from the ease of use? Naively, I'm tempted to use it because it is easy, but down the road I can see maintenance headaches if we decide to move away from JAXB XML unmarshalling.


Your instincts are good - the only situation in which I'd consider adding inline annotations to the schema is if you or your developers were the ones responsible for maintaining the schema itself.

If the schema is someone else's, and there's any danger of it changing in future, then resist the temptation - use an external binding customization. Yes, it's a bit awkward to use, but it's worth the effort.

As for your original issue of name clashes, an XML Schema is not allowed to re-use the same name either. The only way you should be getting name clashes in the generated Java is if you're compiling schemas from multiple namespaces into the same java package. If you have multiple namespaces, I strongly suggest you put each namespace into its own package, it does tend to make things clearer. It also avoids these name clashes.

0

精彩评论

暂无评论...
验证码 换一张
取 消