如何防止 JAXB 转义字符串 [英] How to prevent JAXB escaping a string

查看:62
本文介绍了如何防止 JAXB 转义字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

问题是,JAXB 正在逃避我的 & 符号,因此它生成 £210.我如何让它不这样做?

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

推荐答案

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

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.CharacterEscapeHandler interface.

在 Marshaller 中设置该处理程序

set that handler in the Marshaller

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

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

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

这篇关于如何防止 JAXB 转义字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆