java 告诉 jaxb 不要删除下划线

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

tell jaxb to not remove underscores

javaxmlnaming-conventionsjaxb

提问by KarolDepka


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 about "different names".


有没有办法告诉 JAXB 在从 XML 模式创建 getter/setter 名称时不要删除下划线?
原因:这会导致信息丢失,更难在XML和Java之间切换;例如,在书面交流中,一位参与者可能会对“不同的名字”感到困惑。

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

示例: NR_ROR 不应变为 getNRROR 而是 getNR_ROR。
注意:我相信那些不那么混乱的名字值得“违反”Java 命名约定。
TIA
卡罗尔文

回答by lreeder

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:

创建自定义绑定并将“underscoreBinding”设置为“asCharInWord”。一种方法是创建一个名为 binding.xjb 的文件并使用 -b 标志告诉 xjc 它在哪里。这是您在 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>

回答by Yoni

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

看看 jaxb 提供的 underscoreBinding。例如,参见此处的文档 -> 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.

因为我喜欢camelCaseWords,所以我从未使用过它,但听起来它可以满足您的需求。

回答by cwbarber

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/4512591with lreeder's answer above, changing:

我来到这里是为了寻找如何做同样的事情,并附加了将模式嵌入到 WSDL 中的条件。为此,请将https://stackoverflow.com/a/6545815/4512591与 lreeder 上面的回答结合起来,更改:

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.

这至少与 JAXWS 参考实现有关。