XStream and underscores

孤街醉人 提交于 2019-12-02 20:30:28
Hauke Ingmar Schmidt

XStream uses the underscore to escape characters in identifiers that are valid in Java but invalid in XML (see here). So the underscore itself has to be escaped. You can use a custom NameCoder as described in the FAQ.

That said I normally can get along with the NoNameCoder. But: Don't use underscores in Java property identifiers, if possible; it is untypical for Java and against the Java Naming Conventions.

This worked for me:

XStream xs = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("_-", "_")));

I used:

XmlFriendlyNameCoder nameCoder = new XmlFriendlyNameCoder("ddd", "_");  
XStream xmlStream = new XStream(new Dom4JDriver(nameCoder)); 

and it worked great! I am using x-stream version 1.4.5. Hope it helps!

Worked for me:

XStream xstream = new XStream(new DomDriver("UTF_8", new NoNameCoder()));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!