java - 流异常 : An invalid XML character (Unicode: 0x1a)

标签 java xml

我正在使用 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);
        }
    }
}

当我尝试通过以下代码读取它时,出现异常: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();
            }
        }
    }
}

有什么想法吗?

最佳答案

0x1a 是无效的 xml 字符。无法在 xml 1.0 文档中表示它。

引自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; 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+10000–U+10FFFF: this includes all code points in supplementary planes, including non-characters.

关于java - 流异常 : An invalid XML character (Unicode: 0x1a),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8505816/

相关文章:

java - 在 XML DTD 的枚举属性值中插入特殊字符,如 "#"

java - 对于选项卡式 Activity 中使用 fragment 的 GridView ,适配器为空

java - 如何阅读GC详细信息

java - 无法识别 Netbeans 启动选项 'sun.java2d.opengl=true'

php - 如何使用 PHP 从文档中删除无效的 XML 字符

c# - 如何使用自定义命名空间序列化

java - 处理 xml 文件时的 UTF8 编码无效

java - Android:无法将位图对象绘制到 View 上

java - Joshua Bloch 的 Builder 模式和 PMD 警告

java - 匿名内部类请求澄清