简体   繁体   English

StreamException: 无效的 XML 字符 (Unicode: 0x1a)

[英]StreamException: An invalid XML character (Unicode: 0x1a)

I am using XStream to save the object of user in a file.我正在使用 XStream 将用户的对象保存在文件中。

private void store() {
    XStream xStream = new XStream(new DomDriver("UTF-8"));
    xStream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);

    xStream.alias("configuration", Configuration.class);
    xStream.alias("user", User.class);

    synchronized (ConfigurationDAOImpl.class) {
        try {
            xStream.toXML(configuration, new FileOutputStream(filename.getFile()));
        } catch (IOException e) {
            throw new RuntimeException("Failed to write to " + filename, e);
        }
    }
}

When I am trying to read it by the following code I get an Exception: com.thoughtworks.xstream.io.StreamException: : An invalid XML character (Unicode: 0x1a) was found in the element content of the document.当我尝试通过以下代码读取它时,出现异常:com.thoughtworks.xstream.io.StreamException:: 在文档的元素内容中发现无效的 XML 字符 (Unicode: 0x1a)。

private void lazyLoad() {
    synchronized (ConfigurationDAOImpl.class) {
        // Has the configuration been loaded
        if (configuration == null) {
            if (filename.exists()) {
                try {
                    XStream xStream = new XStream(new DomDriver("UTF-8"));
                    xStream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);

                    xStream.alias("configuration", Configuration.class);
                    xStream.alias("user", User.class);

                    configuration = (Configuration) xStream
                            .fromXML(filename.getInputStream());

                    LOGGER.debug("Loaded configuration from {}.", filename);
                } catch (Exception e) {
                    LOGGER.error("Failed to load configuration.", e);
                }
            } else {
                LOGGER.debug("{} does not exist.", filename);
                LOGGER.debug("Creating blank configuration.");

                configuration = new Configuration();
                configuration.setUsers(new ArrayList<User>());

                // and store it
                store();
            }
        }
    }
}

Any idea?任何的想法?

0x1a is an invalid xml character. 0x1a 是无效的 xml 字符。 There is no way to represent it in an xml 1.0 document.无法在 xml 1.0 文档中表示它。

Quoted from http://en.wikipedia.org/wiki/XML#Valid_characters引自http://en.wikipedia.org/wiki/XML#Valid_characters

Unicode code points in the following ranges are valid in XML 1.0 documents:[9] U+0009, U+000A, U+000D: these are the only C0 controls accepted in XML 1.0;以下范围内的 Unicode 代码点在 XML 1.0 文档中有效:[9] U+0009、U+000A、U+000D:这些是 XML 1.0 中唯一接受的 C0 控件; U+0020–U+D7FF, U+E000–U+FFFD: this excludes some (not all) non-characters in the BMP (all surrogates, U+FFFE and U+FFFF are forbidden); U+0020–U+D7FF、U+E000–U+FFFD:这排除了 BMP 中的一些(不是全部)非字符(所有代理,U+FFFE 和 U+FFFF 都被禁止); U+10000–U+10FFFF: this includes all code points in supplementary planes, including non-characters. U+10000–U+10FFFF:这包括补充平面中的所有代码点,包括非字符。

I replaced 0x1a with a dash character ('-') by the following method:我通过以下方法用破折号字符 ('-') 替换了 0x1a:

/**
 * This method ensures that the output String has only
 * @param in the string that has a non valid character.
 * @return the string that is stripped of the non-valid character
 */
private String stripNonValidXMLCharacters(String in) {      
    if (in == null || ("".equals(in))) return null;
    StringBuffer out = new StringBuffer(in);
    for (int i = 0; i < out.length(); i++) {
        if(out.charAt(i) == 0x1a) {
            out.setCharAt(i, '-');
        }
    }
    return out.toString();
}

As already pointed out, XML 1.0 accepts only a set of characters according to this .正如已经指出的,XML 1.0 根据this只接受一组字符。

Here is a helpful java method to ensure that a string is XML 1.0 conformant, it replaces the invalid ones (all of them not just the 0x1a) with a given replacement.这是一个有用的 java 方法来确保字符串符合 XML 1.0,它用给定的替换替换无效的字符串(所有这些都不仅仅是 0x1a)

public static String replaceInvalidXMLCharacters(String input, String replacement) {
        StringBuffer result = new StringBuffer();
        char currentChar;

        if (input == null || "".equals(input)) {
            return "";
        }
        for (int i = 0; i < input.length(); i++) {
            currentChar = input.charAt(i);
            if (currentChar == 0x9 || currentChar == 0xA || currentChar == 0xD || currentChar >= 0x20 && currentChar <= 0xD7FF || currentChar >= 0xE000
                    && currentChar <= 0xFFFD || currentChar >= 0x10000 && currentChar <= 0x10FFFF) {
                result.append(currentChar);
            } else {
                result.append(replacement);
            }
        }
        return result.toString();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java XML解析器错误从Word复制/粘贴时,无效字符Unicode 0x1A - Java XML parser error Invalid character Unicode 0x1A when copy/paste from Word XmlRpcClientException:发现无效的 XML 字符(Unicode:0x8) - XmlRpcClientException: An invalid XML character (Unicode: 0x8) was found 无效的 XML 字符 (Unicode: 0x0) 错误 - 解决问题 - Invalid XML character(Unicode: 0x0) Error - Solving the Problem 无效的 XML:第 454 行错误:在 CDATA 部分中发现无效的 XML 字符(Unicode:0x8) - Invalid XML: Error on line 454: An invalid XML character (Unicode: 0x8) was found in the CDATA section 如何检查输入的字符串是否包含Unicode:0x1a值 - How to check that entered string contains Unicode: 0x1a value Track.getSimilar:在元素中找到了无效的XML字符(Unicode:0x3)... - Track.getSimilar: An invalid XML character (Unicode: 0x3) was found in the element… 成功编组后在解组时发现无效的 XML 字符 (Unicode: 0x3) - An invalid XML character (Unicode: 0x3) was found on unmarshalling after successful marshalling 在硒文档的元素内容中发现无效的XML字符(Unicode:0x3) - An invalid XML character (Unicode: 0x3) was found in the element content of the document in selenium 发现无效的 XML 字符 (Unicode: 0xc) - An invalid XML character (Unicode: 0xc) was found SolrException:发现无​​效的XML字符(Unicode:0xffffffff) - SolrException: An invalid XML character (Unicode: 0xffffffff) was found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM