【问题标题】:Castor Marshaling :: Invalid XML CharacterCastor Marshaling :: 无效的 XML 字符
【发布时间】:2013-04-24 07:05:52
【问题描述】:

我正在使用 castor API 将对象转换为 XML。

我得到以下异常

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

我知道正确的做法是更正来源,但是这样的无效字符很多。

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

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

【问题讨论】:

  • 再挖掘一下,我发现无效字符只不过是一个退格(ASCII Code = 8)。退格字符如何插入字符串很奇怪。有什么建议吗?
  • 在编组之前对 java 对象内容进行编码,并在解组之后解码。这似乎是解决这个问题的唯一方法。 marshal.setEncoding("base64");使用 base 64 编码和解码。
  • 我认为使用 base64 不合适,因为这不是二进制数据。下面的答案确实有帮助。谢谢。

标签: java xml marshalling castor


【解决方案1】:
 /**
     * 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();
    }  

【讨论】:

    【解决方案2】:

    如果你希望生成的 XML 包含这种类型的

    字符原样

    ,那么 XML 1.1 规范可能会有所帮助。 Castor 可以配置为使用自定义 org.exolab.castor.xml.XMLSerializerFactoryorg.exolab.castor.xml.Serializer 实现编组到 XML 1.1:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      相关资源
      最近更新 更多