xStream转换xml,bean的属性带下划线时,输出为xml时会变成两个下环线。
解决办法: XStream xstream = new XStream(new DomDriver()); 改成: XStream xstream = new XStream(new DomDriver("UTF_8", new NoNameCoder()));
这样好像也行; XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("_-", "_")));
官方解释: http://x-stream.github.io/faq.html#XML_double_underscores
XStream maps Java class names and field names to XML tags or attributes. Unfortunately this mapping cannot be 1:1, since some characters used for identifiers in Java are invalid in XML names.
Therefore XStream uses an XmlFriendlyNameCoder to replace these characters with a replacement. By default this NameCoder uses an underscore as escape character and has therefore to escape the underscore itself also.
You may provide a different configured instance of the XmlFriendlyNameCoder or a complete different implementation like the NoNameCoder to prevent name coding at all. However it is your responsibility then to ensure, that the resulting names are valid for XML.
|