如果你想生成的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