XStream and underscores
Asked Answered
S

5

21

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?

Squash answered 17/2, 2012 at 17:53 Comment(0)
S
12

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.

Supplicatory answered 17/2, 2012 at 18:17 Comment(2)
Thanks very much. XStream(new StaxDriver(new NoNameCoder())) didn't work so I'll investigate making a custom one.Squash
NoNameCoder does work. I just forgot to add @XStreamAlias("a_b") to the variable when I changed it to ab based on your suggestion about java naming conventions.Squash
M
20

This worked for me:

XStream xs = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("_-", "_")));
Makell answered 4/7, 2012 at 19:1 Comment(2)
Worked for me. XmlFriendlyReplacer() is depricated.Futrell
Awesome. Worked great.Lashawnda
S
12

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.

Supplicatory answered 17/2, 2012 at 18:17 Comment(2)
Thanks very much. XStream(new StaxDriver(new NoNameCoder())) didn't work so I'll investigate making a custom one.Squash
NoNameCoder does work. I just forgot to add @XStreamAlias("a_b") to the variable when I changed it to ab based on your suggestion about java naming conventions.Squash
H
5

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!

Hop answered 12/11, 2013 at 13:20 Comment(1)
XStream xs=new XStream (new Dom4JDriver (new NoNameCoder())); works for 1.4.4Washcloth
L
4

Worked for me:

XStream xstream = new XStream(new DomDriver("UTF_8", new NoNameCoder()));
Lingcod answered 14/7, 2015 at 6:56 Comment(1)
works on 1.4.7 only with "UTF8" instead of "UTF_8"Bitterling
S
0

I used this

XStream xstream = new XStream(new DomDriver("UTF_8", new NoNameCoder()));

It worked but it increases the excution time/ parsing time to 4-5minutes

Suffumigate answered 14/9, 2020 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.