开发者

tell jaxb to not remove underscores

开发者 https://www.devze.com 2022-12-18 17:36 出处:网络
Is there a way to tell JAXB to not remove underscores when creating getter/setter names from XML Schema?

Is there a way to tell JAXB to not remove underscores when creating getter/setter names from XML Schema?

Reason: this causes loss of information and it is harder to trip between XML and Java; e.g. in written communications where one participant might be confused abou开发者_如何学Got "different names".

Example: NR_ROR should not become getNRROR but getNR_ROR.

Note: I believe that the less-mangled names are worth the "violation" of Java naming convention.

TIA

karolrvn


Create a custom binding and set "underscoreBinding" to "asCharInWord". One approach is to create a file called binding.xjb and use the -b flag to tell xjc where it is. Here's an example of what you'd put in binding.xml:

<jaxb:bindings
   xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   version="1.0">
   <jaxb:bindings schemaLocation="foobar.xsd">
      <jaxb:globalBindings underscoreBinding="asCharInWord"/>
   </jaxb:bindings>
</jaxb:bindings>


Have a look at the underscoreBinding that jaxb provides. See, for example, in the docs here -> http://java.sun.com/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html

I have never used it since I like camelCaseWords, but it sounds like it does what you are looking for.


I came here looking for how to do the same thing, with the additional qualification that the schema is embedded inside a WSDL. To do that, combine https://stackoverflow.com/a/6545815/4512591 with lreeder's answer above, changing:

schemaLocation="foobar.xsd"

to

wsdlLocation="foobar.wsdl"

Net result:

<jaxb:bindings
   xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   version="1.0">
   <jaxb:bindings wsdlLocation="foobar.wsdl">
      <jaxb:globalBindings underscoreBinding="asCharInWord"/>
   </jaxb:bindings>
</jaxb:bindings>

This is with the JAXWS Reference Implementation, at least.

0

精彩评论

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

关注公众号