Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java StreamException:无效的XML字符(Unicode:0x1a)_Java_Xml - Fatal编程技术网

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

Java StreamException:无效的XML字符(Unicode:0x1a),java,xml,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", U

我正在使用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();
            }
        }
    }
}
private void lazyLoad(){
已同步(ConfigurationDAOImpl.class){
//配置是否已加载
if(配置==null){
if(filename.exists()){
试一试{
XStream XStream=新的XStream(新的DomDriver(“UTF-8”);
setMode(xStream.XPATH_绝对_引用);
别名(“配置”,configuration.class);
别名(“user”,user.class);
配置=(配置)xStream
.fromXML(filename.getInputStream());
debug(“从{}加载的配置,文件名);
}捕获(例外e){
LOGGER.错误(“加载配置失败”,e);
}
}否则{
LOGGER.debug(“{}不存在。”,文件名);
debug(“创建空白配置”);
配置=新配置();
setUsers(新的ArrayList());
//并储存它
store();
}
}
}
}

有什么想法吗?

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

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();
            }
        }
    }
}
引自

以下范围内的Unicode代码点在XML 1.0中有效 文档:[9]U+0009、U+000A、U+000D:这些是唯一的C0控件 在XML1.0中被接受;U+0020–U+D7FF,U+E000–U+FFFD:这不包括一些 (并非所有)BMP中的非字符(所有代理,U+FFFE和U+FFFF 禁止使用);U+10000–U+10FFFF:这包括中的所有代码点 补充平面,包括非字符


我使用以下方法将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();
}
/**
*此方法确保输出字符串仅具有
*@param包含无效字符的字符串。
*@返回去掉无效字符的字符串
*/
私有字符串stripNonValidXMLCharacters(字符串位于{
if(in==null | |(“”.equals(in)))返回null;
StringBuffer out=新的StringBuffer(输入);
for(int i=0;i
正如前面所指出的,XML1.0只接受一组字符

下面是一个有用的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();
    }
publicstaticstringreplaceInvalidXmlCharacters(字符串输入,字符串替换){
StringBuffer结果=新的StringBuffer();
字符当前字符;
如果(输入==null | |“”.equals(输入)){
返回“”;
}
对于(int i=0;i=0x20&¤tChar=0xE000

&¤tChar=0x10000&¤tChar可能相关:感谢您的回答。我用短划线字符('-')替换了0x1a从某种意义上说。你应该接受乔特罗的答案作为正确答案,他回答了你的问题并给出了很好的解释。认真地接受另一个答案。一些主持人会在这里生气的。因为你应该接受乔特罗的答案,而不是提出你自己的答案。