Hi, sc wrote:
> Hi, > > I am facing an xstream issue that I just cant figure out a solution to. An > help is much appreciated. > > I have an XML that looks like > > <parent> > .... > .... > .... > <child_son_age>10</child_son_age> > <child_daughter_age>10</child_daughter_age> > .... > .... > </parent> > > This XML needs to be converted to the following Object hierarchy: > > class parent { > private .... > private .... > private .... > private Child child; > private .... > private .... > } > class Child { > private int sonAge; > private int daughterAge; > } > > > I cannot change the format of the XML as it is coming from another system. > > Also having it converted to something like the following is NOT an option > either.: > class parent { > private .... > private .... > private .... > private int childSonAge; > private int childDaughterAge; > private .... > private .... > } > > I spend quiet a bit of time looking for a solution online but just cant > get it to work. XStream is basically from Java to XML and back, i.e. the Java object graph dictates the XML structure. Therefore you will not find any configuration possibility or other automatism in XStream to flatten the Child element into two XML elements of the parent. You will have to write a custom converter for your parent class. However, that's quite easy, have a look at the converter tutorial: http://xstream.codehaus.org/converter-tutorial.html Then a major rule of thumb: Always serialize your objects to XML and tweak the output until it matches the XML you'd like to read. Anything else will be an annoying experience, because then you will have to guess on your own what XML format XStream currently understands. Looking at your XML it seems also that you will have a general mapping from underscore separated words to camel case (and vice versa). Have a look at com.thoughtworks.acceptance.CustomMapperTest.FieldPrefixStrippingMapper how to implement such a general name mapping with a custom Mapper. Also, since underscores are involved, look at FAQ: http://xstream.codehaus.org/faq.html#XML_double_underscores Cheers, Jörg --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email