我有一个XmlObject,它具有我需要的正确值。 例如:1½-2Y
但是当我试图将其转换为流的字节时,我看到的结果是1½-2Y。
示例代码:
import org.apache.xmlbeans.XmlObject;
Class MyClass implements XmlBuilder<T> {
protected final String serializeToXml(XmlObject xmlObject) {
ByteArrayOutputStream os = null;
try {
os = new ByteArrayOutputStream();
xmlObject.save(os,createXmlOptions()); /Its adding a special char here
return os.toString();
}
}
protected final XmlOptions createXmlOptions() {
final XmlOptions xmlOptions = new XmlOptions();
xmlOptions.setValidateOnSet();
xmlOptions.setCharacterEncoding(UTF_8_ENCODING);
return xmlOptions;
}
}
答案 0 :(得分:0)
os.toString()
将在内部调用new String(buffer)
,因此将使用我认为不 UTF-8的系统编码。
通常,您应该明确提供编码,例如: new String( os.toByteArray(), "UTF-8")
。