如何在XStream中禁用不必要的转义?

时间:2023-01-13 13:27:00

XStream by default unnecessarily escapes >," ... etc.

XStream默认情况下不必要地转义>,“…”等。

Is there a way to disable this (and only escape <, &)?

有没有一种方法可以禁用这个(并且只使用escape <, &)?

3 个解决方案

#1


7  

This is the result of the default PrettyPrintWriter. Personally, I like to escape both < and >. It makes the output look more balanced.

这是默认的PrettyPrintWriter的结果。就我个人而言,我喜欢同时避免 <和> 。它使输出看起来更加平衡。

If you want canonicalized XML output, you should use the C14N API provided in Java.

如果想要规范化的XML输出,应该使用Java中提供的C14N API。

If the streamed content includes XML, CDATA is a better option. Here is how I did it,

如果流内容包含XML,那么CDATA是更好的选择。我是这样做的,

XStream xstream = new XStream(
           new DomDriver() {
               public HierarchicalStreamWriter createWriter(Writer out) {
                   return new MyWriter(out);}});
String xml = xstream.toXML(myObj);

    ......

public class MyWriter extends PrettyPrintWriter {
    public MyWriter(Writer writer) {
        super(writer);
    }

    protected void writeText(QuickWriter writer, String text) { 
        if (text.indexOf('<') < 0) {
            writer.write(text);
        }
        else { 
            writer.write("<[CDATA["); writer.write(text); writer.write("]]>"); 
        }
    }
}

#2


1  

Cdata does not worked for me, Finally i have to work with Apache StringUtils.

Cdata对我不起作用,最后我不得不使用Apache StringUtils。

StringUtils.replaceEach(xml, new String[]{"&lt;","&quot;","&apos;","&gt;"}, new String[]{"<","\"","'",">"});

#3


0  

XStream doesn't write XML on its own, it uses various libs ("drivers"?) to do so.

XStream本身并不编写XML,而是使用各种libs(“驱动程序”?)来编写XML。

Just choose one which doesn't. The list is on their site. I guess it would use XOM by default.

只选一个没有的。名单在他们的网站上。我猜它会默认使用XOM。

#1


7  

This is the result of the default PrettyPrintWriter. Personally, I like to escape both < and >. It makes the output look more balanced.

这是默认的PrettyPrintWriter的结果。就我个人而言,我喜欢同时避免 <和> 。它使输出看起来更加平衡。

If you want canonicalized XML output, you should use the C14N API provided in Java.

如果想要规范化的XML输出,应该使用Java中提供的C14N API。

If the streamed content includes XML, CDATA is a better option. Here is how I did it,

如果流内容包含XML,那么CDATA是更好的选择。我是这样做的,

XStream xstream = new XStream(
           new DomDriver() {
               public HierarchicalStreamWriter createWriter(Writer out) {
                   return new MyWriter(out);}});
String xml = xstream.toXML(myObj);

    ......

public class MyWriter extends PrettyPrintWriter {
    public MyWriter(Writer writer) {
        super(writer);
    }

    protected void writeText(QuickWriter writer, String text) { 
        if (text.indexOf('<') < 0) {
            writer.write(text);
        }
        else { 
            writer.write("<[CDATA["); writer.write(text); writer.write("]]>"); 
        }
    }
}

#2


1  

Cdata does not worked for me, Finally i have to work with Apache StringUtils.

Cdata对我不起作用,最后我不得不使用Apache StringUtils。

StringUtils.replaceEach(xml, new String[]{"&lt;","&quot;","&apos;","&gt;"}, new String[]{"<","\"","'",">"});

#3


0  

XStream doesn't write XML on its own, it uses various libs ("drivers"?) to do so.

XStream本身并不编写XML,而是使用各种libs(“驱动程序”?)来编写XML。

Just choose one which doesn't. The list is on their site. I guess it would use XOM by default.

只选一个没有的。名单在他们的网站上。我猜它会默认使用XOM。