我正在使用XStream將用戶的對象保存在文件中。StreamException:無效的XML字符(Unicode:0x1a)
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();
}
}
}
}
任何想法?
可能相關:http://stackoverflow.com/a/8427929/486504 – 2011-12-14 14:38:08