2013-04-24 64 views
0

我正在使用castor将对象转换为XML的API。Castor Marshaling ::无效的XML字符

我得到下面的异常

产生的原因:org.xml.sax.SAXException:字符 '' 是无效的XML字符。

我知道正确的方法是纠正源,但有很多这样的无效字符。

在另一个论坛中,有人建议在编组java之前对java对象内容进行编码,然后对输出进行解码(Base64)。该方法看起来非常麻烦,并且不适合该解决方案。

我需要一种方法来在编组过程中跳过这些字符,并且生成的XML应该包含字符原样。

+0

挖掘了一下之后,我发现无效字符只不过是一个退格符(ASCII码= 8)。奇怪的是,退格字符是如何插入字符串的。 有什么建议吗? – Taran 2013-04-24 14:20:35

+0

在编组它们之前对java对象内容进行编码,并在解组之后进行解码。这似乎是解决这个问题的唯一方法。 marshal.setEncoding( “BASE64”);使用base 64编码和解码。 – constantlearner 2013-04-25 21:25:15

+0

我不认为使用base64是合适的,因为这不是二进制数据。下面的答案确实有帮助。谢谢。 – Taran 2013-04-26 11:56:51

回答

0
/** 
    * This method ensures that the output String has only 
    * valid XML unicode characters as specified by the 
    * XML 1.0 standard. For reference, please see 
    * <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char">the 
    * standard</a>. This method will return an empty 
    * String if the input is null or empty. 
    * 
    * @param in The String whose non-valid characters we want to remove. 
    * @return The in String, stripped of non-valid characters. 
    */ 
    public String stripNonValidXMLCharacters(String in) { 
     StringBuffer out = new StringBuffer(); // Used to hold the output. 
     char current; // Used to reference the current character. 

     if (in == null || ("".equals(in))) return ""; // vacancy test. 
     for (int i = 0; i < in.length(); i++) { 
      current = in.charAt(i); // NOTE: No IndexOutOfBoundsException caught here; it should not happen. 
      if ((current == 0x9) || 
       (current == 0xA) || 
       (current == 0xD) || 
       ((current >= 0x20) && (current <= 0xD7FF)) || 
       ((current >= 0xE000) && (current <= 0xFFFD)) || 
       ((current >= 0x10000) && (current <= 0x10FFFF))) 
       out.append(current); 
     } 
     return out.toString(); 
    } 
0

如果你想生成的XML遏制这种

字符,因为它是

,那么XML 1.1规范可能的帮助。 蓖麻可以被配置为编组到XML 1.1用定制org.exolab.castor.xml.XMLSerializerFactoryorg.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 

或通过您的特定CastorMarshallersetCastorProperties方法设置这两个属性。

但是,请注意XML 1.1 is not accepted by browsersnot all XML parsers can parse XML 1.1 out of the box