Java 如何防止 JAXB 转义字符串

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

How to prevent JAXB escaping a string

javajaxb

提问by rjsang

I have an object, generated by XJC, called Product. I want to set product.currentPrice (a String)to be £210where £is the currency symbol (passed in from elsewhere in the system).

我有一个由 XJC 生成的对象,名为Product. 我想设置货币符号product.currentPrice (a String)£210哪里£(从系统中的其他地方传入)。

Trouble is, JAXB is escaping my ampersand, so it produces £210instead. How do I make it not do this?

问题是,JAXB 正在逃避我的 & 符号,所以它产生了£210。我如何让它不这样做?

采纳答案by Manish Singh

By default, the marshaller implementation of the JAXB usually escapes characters. To change this default behavior you will have to Write a class that implements the com.sun.xml.bind.marshaller.CharacterEscapeHandlerinterface.

默认情况下,JAXB 的编组器实现通常会转义字符。要更改此默认行为,您必须编写一个实现该com.sun.xml.bind.marshaller.CharacterEscapeHandler接口的类。

set that handler in the Marshaller

在 Marshaller 中设置该处理程序

CharacterEscapeHandler escapeHandler = NoEscapeHandler.theInstance;
marshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", escapeHandler); 

You can have a look at samples provided with JAXB, character escaping sample

您可以查看 JAXB 提供的示例,字符转义示例

回答by bdoughan

How about using a CDATA section to wrap the value, and then use a JAXB implementation such as EclipseLink MOXythat can handle CDATA?

如何使用 CDATA 部分来包装值,然后使用 JAXB 实现,例如可以处理 CDATA 的EclipseLink MOXy

To handle CDATA using MOXy see:

要使用 MOXy 处理 CDATA,请参阅:

回答by fred

Depending on what you are exactly looking for you can either :

根据您正在寻找的内容,您可以:

  • disable character escaping
  • or use CDATA string which support can be added into JAXB with just a bit of configuration
  • 禁用字符转义
  • 或使用 CDATA 字符串,只需稍加配置即可将其支持添加到 JAXB 中

回答by jfuentes

Like rjsang said, there is a bug when using "UTF-8" encoding, which is the default. If you don't care about the case sensitive issues, try using "utf-8" and get your custom escaper to work as it is supposed to do. Also UTF-16 works, anything but "UTF-8": "uTf-8" and so on.

就像 rjsang 所说,使用“UTF-8”编码时存在一个错误,这是默认值。如果您不关心区分大小写的问题,请尝试使用“utf-8”并让您的自定义转义符按预期工作。UTF-16 也可以工作,除了“UTF-8”:“uTf-8”等等。

Awful bug in the standard.

标准中的可怕错误。

Here is the code to do so:

这是执行此操作的代码:

marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "utf-8");

回答by Carlitos Way

If you implement this interface: com.sun.xml.bind.marshaller.CharacterEscapeHandler, with your own character escaping, but you want to delegate some behaviour to the traditional escaping, you can use any of these clases (or write your own code based on them):

如果你实现了这个接口:com.sun.xml.bind.marshaller.CharacterEscapeHandler,用你自己的字符转义,但你想将一些行为委托给传统的转义,你可以使用这些类中的任何一个(或编写你自己的基于代码的代码)在他们):

  • com.sun.xml.bind.marshaller.MinimumEscapeHandler
  • com.sun.xml.bind.marshaller.NioEscapeHandler
  • com.sun.xml.bind.marshaller.DumbEscapeHandler
  • com.sun.xml.bind.marshaller.MinimumEscapeHandler
  • com.sun.xml.bind.marshaller.NioEscapeHandler
  • com.sun.xml.bind.marshaller.DumbEscapeHandler

They come with the JAXB RI Implementation AKA jaxb-impl-2.2.1.jar.

它们带有 JAXB RI 实现 AKA jaxb-impl-2.2.1.jar。

Hope this help to anyone, like it helped me.

希望这对任何人都有帮助,就像它帮助了我一样。

回答by ZiglioUK

Solution for XmlStreamWriter

XmlStreamWriter 解决方案

final XMLOutputFactory streamWriterFactory = XMLOutputFactory.newFactory();
streamWriterFactory.setProperty("escapeCharacters", false);

From here

从这里