Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XStream and underscores

Tags:

It looks like XStream (com.thoughtworks.xstream -> xstream 1.4.2) is handling underscores in element and attribute names in a very strange way. I need to fetch and parse an xml from a customer that are having underscores in their attributes. This is my first try with XStream and I'm a bit disappointed as I was hoping to avoid all the ugly xml parsing.

Here I provide a small test sample to hi light the behaviour. The last example shows my problem.

public class MyTest {   public void testIt() {     C1 a = new C1();     a.a_b= "a_b";      XStream xstream = new XStream();     xstream.processAnnotations(C1.class);      String xml = xstream.toXML(a);     Logger.info(xml);      C1 b = (C1) xstream.fromXML(xml);     Logger.info(b.a_b);      C1 c = (C1) xstream.fromXML("<C1 a_b=\"a_b\"/>");     Logger.info(c.a_b);   } }  @XStreamAlias("C1") class C1 { @XStreamAsAttribute String a_b; } 

This outputs

INFO: <C1 a__b="a_b"/> INFO: a_b INFO: null 

Now my question - is there a way to make XStream understand a single underscore?

like image 470
eigil Avatar asked Feb 17 '12 17:02

eigil


People also ask

Is XStream thread safe?

For most use cases, the XStream instance is thread-safe, once configured (there are caveats when using annotations) Clear messages are provided during exception handling to help diagnose issues. Starting with version 1.4.

What is XStream used for?

XStream library XStream uses reflection to discover the structure of the object graph to serialize at run time, and doesn't require modifications to objects. It can serialize internal fields, including private and final, and supports non-public and inner classes.

What is XStream alias?

Advertisements. Class aliasing is used to create an alias of a fully qualified name of a class in XML.


1 Answers

This worked for me:

XStream xs = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("_-", "_"))); 
like image 151
James Young Avatar answered Oct 08 '22 13:10

James Young