找到了无效的XML字符(Unicode:0xc)

时间:2011-04-21 10:05:44

标签: java xml dom xml-parsing

使用Java DOM解析器解析XML文件会导致:

[Fatal Error] os__flag_8c.xml:103:135: An invalid XML character (Unicode: 0xc) was found in the element content of the document.
org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0xc) was found in the element content of the document.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)

10 个答案:

答案 0 :(得分:40)

即使将数据封装在CDATA块中,XML文档中也有一些不允许的字符。

如果您生成了文档,则需要实体对其进行编码或将其删除。如果您有错误的文档,则应在尝试解析之前删除这些字符。

请参阅此主题中的dolmens回答:Invalid Characters in XML

他链接到这篇文章:http://www.w3.org/TR/xml/#charsets

基本上,不允许使用低于0x20的所有字符,除了0x9(TAB),0xA(CR?),0xD(LF?)

答案 1 :(得分:8)

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 :(得分:6)

字符0x0C在XML 1.0中无效,但可能是valid character in XML 1.1。因此,除非xml文件在prolog中将版本指定为1.1,否则它将无效,您应该向该文件的生产者投诉。

答案 3 :(得分:2)

答案 4 :(得分:2)

每当无效的xml字符出现xml时,就会出现这样的错误。当你在记事本++中打开它时,它看起来像VT,SOH,FF这些都是无效的xml字符。我使用的是xml版本1.0,我在通过模式

在数据库中输入之前验证了文本数据
Pattern p = Pattern.compile("[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\u10000-\u10FFF]+"); 
retunContent = p.matcher(retunContent).replaceAll("");

它将确保在xml

中不会输入无效的特殊字符

答案 5 :(得分:2)

您可以使用自定义FilterReader类过滤所有“无效”字符:

public class InvalidXmlCharacterFilter extends FilterReader {

    protected InvalidXmlCharacterFilter(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 read;

        for (int i = off; i < off + read; i++) {
            if (!XMLChar.isValid(cbuf[i])) cbuf[i] = '?';
        }
        return read;
    }
}

然后像这样运行:

InputStream fileStream = new FileInputStream(xmlFile);
Reader reader = new BufferedReader(new InputStreamReader(fileStream, charset));
InvalidXmlCharacterFilter filter = new InvalidXmlCharacterFilter(reader);
InputSource is = new InputSource(filter);
xmlReader.parse(is);

答案 6 :(得分:0)

我遇到了类似的问题,其中XML包含控制字符。在查看代码之后,我发现一个弃用的类StringBufferInputStream用于读取字符串内容。

http://docs.oracle.com/javase/7/docs/api/java/io/StringBufferInputStream.html

This class does not properly convert characters into bytes. As of JDK 1.1, the preferred way to create a stream from a string is via the StringReader class.

我把它改成了ByteArrayInputStream,它工作正常。

答案 7 :(得分:0)

对于将字节数组读入String并尝试使用JAXB转换为对象的人,可以通过从字节数组创建String来添加“iso-8859-1”编码,如下所示:

String JAXBallowedString = new String(byte [] input,“iso-8859-1”);

这会将冲突的字节替换为JAXB可以处理的单字节编码。显然这个解决方案只是解析xml。

答案 8 :(得分:0)

所有这些答案似乎都假定用户正在生成错误的XML,而不是从应该更了解的gSOAP接收它!

答案 9 :(得分:0)

今天,我遇到了类似的错误:

Servlet.service() for servlet [remoting] in context with path [/***] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: buildDocument failed.] with root cause org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 91; An invalid XML character (Unicode: 0xc) was found in the value of attribute "text" and element is "label".


在遇到第一个错误后,我手动重新输入了整行,因此无法潜入特殊字符,并且Notepad ++不会显示任何不可打印的字符(黑色白色),但是我一遍又一遍地遇到相同的错误。

当我查看自己所做的工作与以前的工作不同时,结果是在结束/>之前又增加了一个空格(正如我所听说的建议用于较早的解析器,但不应做任何其他事情)无论如何,都与XML标准有所不同):

<label text="this label's text" layout="cell 0 0, align left" />

当我删除空格时:

<label text="this label's text" layout="cell 0 0, align left"/>

一切正常。


因此,这绝对是一个误导性的错误消息。