// shall I write some keywords here to boost search engine ranking?

Saturday, November 01, 2008

Duplicate Underscore in XML generated by XStream

When using XStream to serialise Java object into XML, alias name with undercore (_) will get duplicate. For example, "first_name" will be serialised as "first__name".

The reason is explained in their FAQ and the workaround is to provide your own XmlFriendlyReplacer. However, it do not give any example. So here is the example:


// replace $ with "ddd"
// replace underscore with underscore
XmlFriendlyReplacer replacer = new XmlFriendlyReplacer("ddd", "_");
XStream xstream = new XStream(new DomDriver("UTF-8", replacer));

21 comments:

Unknown said...

Excellent Thiam - just what I was looking for.

Many thanks!

Anonymous said...

you the man! that worked great

Anonymous said...

Perfection! thanks a lot

Unknown said...

To use the default XPP Driver, though, you want to change it to:

XStream xstream = new XStream(new XppDriver(replacer));

Anonymous said...
This comment has been removed by a blog administrator.
Gary Rowe said...

Good result! Thanks very much for posting.

Anonymous said...

thank you so much... You can't imagine how in pain I was planning to go...

Anonymous said...

thank you so much... You can't imagine how in pain I was planning to go...

Jide B said...

Instant solution. Many thanks

Unknown said...

hey, so much thanks! great post!

Anonymous said...

perfect!

Anonymous said...

Thanks Thiam - works like a charm

Anonymous said...

It was great. Thanks

Anonymous said...

It was great. Thanks

Anonymous said...

very nice example 10x

Anonymous said...

superb work done, Thanks

Anonymous said...

Does not work for me... I get double underscore with setting a modified XmlFriendlyReplacer to XStream object. Using 1.3.1 of XStream.

Anonymous said...

Thanks a tonne !

Anonymous said...

worked for me, in version 1.4.2, thank you

Anonymous said...

Thx

Anonymous said...

thanks, but doesn't work on 1.3.1