Unmarshall期间无效的XML字符

时间:2011-04-28 07:24:52

标签: java xml-serialization jaxb unmarshalling

我使用编码“UTF-8”将对象编组到XML文件。它成功生成文件。但是当我试图将它解组时,会出现错误:

  

无效的XML字符(Unicode:   在{的值中找到0x {2})   属性“{1}”,元素为“0”

字符为0x1A或\ u001a,在UTF-8中有效,但在XML中是非法的。 JAXB中的Marshaller允许将此字符写入XML文件,但Unmarshaller无法解析它。我尝试使用其他编码(UTF-16,ASCII等),但仍然是错误。

常见的解决方案是在XML解析之前删除/替换此无效字符。但是如果我们需要这个角色,那么如何在解组后获得原始角色?


在寻找此解决方案时,我想在解组之前用替换字符替换无效字符(例如dot =“。”)。

我创建了这个类:

public class InvalidXMLCharacterFilterReader extends FilterReader {

    public static final char substitute = '.'; 

    public InvalidXMLCharacterFilterReader(Reader in) {
        super(in);
    }

    @Override
    public int read(char[] cbuf, int off, int len) throws IOException {

        int read = super.read(cbuf, off, len);

        if (read == -1)
            return -1;

        for (int readPos = off; readPos < off + read; readPos++) {
            if(!isValid(cbuf[readPos])) {
                   cbuf[readPos] = substitute;
            }
        }

        return readPos - off + 1; 
    }

    public boolean isValid(char c) {
        if((c == 0x9)
                || (c == 0xA) 
                || (c == 0xD) 
                || ((c >= 0x20) && (c <= 0xD7FF)) 
                || ((c >= 0xE000) && (c <= 0xFFFD)) 
                || ((c >= 0x10000) && (c <= 0x10FFFF)))
        {
            return true;
        } else
            return false;
    }
 }

然后这就是我阅读和解组文件的方式:

FileReader fileReader = new FileReader(this.getFile());
Reader reader = new InvalidXMLCharacterFilterReader(fileReader);
Object o = (Object)um.unmarshal(reader);

不知何故,读者不会用我想要的字符替换无效字符。它导致错误的XML数据无法解组。我的InvalidXMLCharacterFilterReader类有问题吗?

2 个答案:

答案 0 :(得分:3)

Unicode字符U + 001A是illegal in XML 1.0

用于表示它的编码在这种情况下无关紧要,在XML内容中根本不允许。

要包含

XML 1.1 allows some of the restricted characters(包括U + 001A),但必须作为数字字符引用(&#x1a;

维基百科有a nice summary of the situation

答案 1 :(得分:2)

我认为主要问题是在编组期间逃避非法字符。提到类似的东西here,你可以尝试一下。

它提供将编码更改为Unicode marshaller.setProperty("jaxb.encoding", "Unicode");