gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:47:12 27 4
gpt4 key购买 nike

我正在使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com