我想给我的位置标记添加一个描述,这是一系列的html。当我运行编组程序时,我得到一堆特殊的字符串,而不是特殊的字符。也就是说,我的最终文件看起来不像CDATA<html>,而像CDATA<html>。
我不想覆盖JAK编组程序,所以我希望有一种简单的方法来确保我的字符串被带到文件中。
谢谢。
发布于 2013-11-26 23:16:25
封送处理实际上转义了特殊字符,"转换为",&转换为&,<转换为<。
我的建议是使用字符串的replace函数,它实际上有助于将转义字符重新转换回正常字符。
try {
StringWriter sw = new StringWriter();
return marshaller.marshal(obj, sw);
} catch (JAXBException jaxbe) {
throw new XMLMarshalException(jaxbe);
}使用sw对象,使用sw.toString().replace()将更改后的字符替换回原始字符。
这将确保你拥有与你想要的东西同步的东西。
希望这能帮上忙..
发布于 2015-12-03 05:48:51
创建实现CharacterEscapeHandler的NoEscapeHandler (查看com.sun.xml.bind.marshaller.DumbEscapeHandler示例
import java.io.IOException;
import java.io.Writer;
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
public class NoEscapeHandler implements CharacterEscapeHandler {
private NoEscapeHandler() {}
public static final CharacterEscapeHandler theInstance = new NoEscapeHandler();
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
int limit = start+length;
for (int i = start; i < limit; i++) {
out.write(ch[i]);
}
}
}然后设置封送拆收器的属性。
marshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", NoEscapeHandler.theInstance);或者使用DataWriter
StringWriter sw = new StringWriter();
DataWriter dw = new DataWriter(sw, "utf-8", NoEscapeHandler.theInstance);使用XmlStreamWriter和jaxb框架时的
streamWriterFactory.setProperty("escapeCharacters",XMLOutputFactory streamWriterFactory = XMLOutputFactory.newFactory();final false);
https://stackoverflow.com/questions/20220438
复制相似问题