如何避免Xstream生成带有& amp或& quote或类似字符的xml文件? [英] How to avoid Xstream to produce xml file with &amp or &quote or similar chars?

查看:79
本文介绍了如何避免Xstream生成带有& amp或& quote或类似字符的xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在Java中使用 Xstream .
我有一个名为 CarList 的汽车列表.
我有一辆汽车作为名为 Car 的对象.

I start to work with Xstream with Java.
I have a list of cars named CarList.
I have a car as an object named Car.

XStream xstream = new XStream(new StaxDriver());
xstream.alias("Car", Car.class);
xstream.alias("Cars", CarList.class);
xstream.addImplicitCollection(CarList.class, "list");
xstream.toXML(list, new FileWriter(fileName+xmlExtension));

不幸的是,每次我保存文件时.我的XML包含& quot & ,而不是"& .
我究竟做错了什么?我应该使用正则表达式替换那些字符还是有更好的选择?

Unfortunately each time I save my file. My XML contains &quot and &amp instead of " and &.
What am I doing wrong? Should I use a regex for replacing those characters or there is something better to do?

推荐答案

我发现这个问题很有趣,因此我对此进行了一些调查.

I found this problem quite intersting, so I made a little investigation about it.

XStream不会自行编写XML,而是使用各种库(驱动程序"?)来编写.因此,可以在使用 HierarchicalStreamDriver 的具体实现中找到原因.

XStream doesn't write XML on its own, it uses various libs ("drivers"?) to do so. Therefore, the reason could be found in concrete implemenation of using HierarchicalStreamDriver.

首先,我尝试使用 XStream xstream = new XStream()进行调试.在这种情况下,您可以看到一个 switch 操作符来编写这些替换项(请参见 PrettyPrintWriter.writeText()):

First of all, I have tried to debug with XStream xstream = new XStream(). In this case you can see a switch operatior to write these replacements (see PrettyPrintWriter.writeText()):

private void writeText(String text) {
    int length = text.length();
    for (int i = 0; i < length; i++) {
        char c = text.charAt(i);
        switch (c) {
            case '\0':
                this.writer.write(NULL);
                break;
            case '&':
                this.writer.write(AMP);
                break;
            case '<':
                this.writer.write(LT);
                break;
            case '>':
                this.writer.write(GT);
                break;
            case '"':
                this.writer.write(QUOT);
                break;
            case '\'':
                this.writer.write(APOS);
                break;
            case '\r':
                this.writer.write(SLASH_R);
                break;
            default:
                this.writer.write(c);
        }
    }
}

第二,我在您的示例中尝试使用 XStream xstream = new XStream(driver).具有可用资源(我没有更深入地了解)的最终调试点是 StaxWriter.setValue().这意味着,原因不在于 XStream 本身,而是在用于编写xml文件的 XMLStreamWriter 中.我发现 XMLStreamWriter writeCharacters没有转义.我已经尝试了一种给定的解决方案: streamWriterFactory.setProperty("escapeCharacters",false); ,我当前的解决方案是:

Second, I have tried that with your example with XStream xstream = new XStream(driver). The final debug point with available sources (I did not go deeper) is StaxWriter.setValue(). It means, that reason is not in XStream itself, but in the XMLStreamWriter that is used to write a xml file. I have found XMLStreamWriter writeCharacters without escaping . I have tried one given solution: streamWriterFactory.setProperty("escapeCharacters", false); and my current solution is:

StaxDriver driver = new StaxDriver();
driver.getOutputFactory().setProperty("escapeCharacters", false);
XStream xstream = new XStream(driver);

我已经解决了替换"& 的问题.但是问题是,如果您有一个<> 的字符串-也不会更改,并且会破坏最终的xml.

I have fixed the problem of replacing " and &. But problem is that if you have e.g. <> in one of your strings - they will not be changed as well, and it breaks the final xml.

所以,我不知道您的情况,这个简单的解决方案可能对您来说就足够了.如果没有,并且输出中有<> 符号,那么我会找到一个驱动程序读取 escapeCharacters 属性的地方,并找出如何仅禁用特定的转义集字符.

So, I do not know your context, probably this light solution could be enough for you. If not and you have <> symbols you your output, then I would find a place where driver reads escapeCharacters property and find out how to disable only specific set of escape characters.

我不知道是否有任何标准方法,对不起.但是看来我发现的所有结果都不是通用的,并且要解决这个问题,我们必须了解任务上下文.正如您提到的,不错的解决方案之一就是替换最终xml中所需的转义符号:如何在XStream中禁用不必要的转义?.

I do not know is there any standard way or not, sorry. But it seems that all results I have found are not universal and to solve this correct, we have to know a task context. One of not bad solution, as you mentioned, is just replace required escape symbols in final xml: XStream apostrophe Issue in converting Java Object to XML and How can I disable unnecessary escaping in XStream? .

这篇关于如何避免Xstream生成带有&amp; amp或&amp; quote或类似字符的xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆