0

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

我得到以下异常

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

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

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

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

4

2 回答 2

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();
    }  
于 2013-04-25T21:28:55.390 回答
0

如果您希望生成的 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

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

但是请注意,浏览器不接受 XML 1.1,并且并非所有 XML 解析器都可以立即解析 XML 1.1

于 2017-01-19T20:51:32.907 回答