如果你想生成的XML遏制这种
字符,因为它是
,那么XML 1.1规范可能的帮助。 蓖麻可以被配置为编组到XML 1.1用定制org.exolab.castor.xml.XMLSerializerFactory
和org.exolab.castor.xml.Serializer
实现:
package com.foo.castor;
......
import org.exolab.castor.xml.BaseXercesOutputFormat;
import org.exolab.castor.xml.Serializer;
import org.exolab.castor.xml.XMLSerializerFactory;
import org.xml.sax.DocumentHandler;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XML11Serializer;
@SuppressWarnings("deprecation")
public class CastorXml11SerializerFactory implements XMLSerializerFactory {
private static class CastorXml11OutputFormat extends BaseXercesOutputFormat{
public CastorXml11OutputFormat(){
super._outputFormat = new OutputFormat();
}
}
private static class CastorXml11Serializer implements Serializer {
private XML11Serializer serializer = new XML11Serializer();
@Override
public void setOutputCharStream(Writer out) {
serializer.setOutputCharStream(out);
}
@Override
public DocumentHandler asDocumentHandler() throws IOException {
return serializer.asDocumentHandler();
}
@Override
public void setOutputFormat(org.exolab.castor.xml.OutputFormat format) {
serializer.setOutputFormat((OutputFormat)format.getFormat());
}
@Override
public void setOutputByteStream(OutputStream output) {
serializer.setOutputByteStream(output);
}
}
@Override
public Serializer getSerializer() {
return new CastorXml11Serializer();
}
@Override
public org.exolab.castor.xml.OutputFormat getOutputFormat() {
return new CastorXml11OutputFormat();
}
}
在castor.properties
文件全局
org.exolab.castor.xml.serializer.factory=com.foo.castor.CastorXml11SerializerFactory
org.exolab.castor.xml.version=1.1
或通过您的特定CastorMarshaller
的setCastorProperties
方法设置这两个属性。
但是,请注意XML 1.1 is not accepted by browsers和not all XML parsers can parse XML 1.1 out of the box。
挖掘了一下之后,我发现无效字符只不过是一个退格符(ASCII码= 8)。奇怪的是,退格字符是如何插入字符串的。 有什么建议吗? – Taran 2013-04-24 14:20:35
在编组它们之前对java对象内容进行编码,并在解组之后进行解码。这似乎是解决这个问题的唯一方法。 marshal.setEncoding( “BASE64”);使用base 64编码和解码。 – constantlearner 2013-04-25 21:25:15
我不认为使用base64是合适的,因为这不是二进制数据。下面的答案确实有帮助。谢谢。 – Taran 2013-04-26 11:56:51