Hi Ihar, Ihar Tsimoshka wrote:
> I need to generate XML from java objects on Android. XML nodes must be in > definite sequence. > Due http://xstream.codehaus.org/manual-tweaking-output.html order of XML > nodes match object's fields define. There is no problems when I use java > classes(String, Date...) as fields. But there is problem when I need > serialize my objects as fields. > > Here is my code: > > final XStream x = new XStream(); > > x.autodetectAnnotations(true); > > SecondEntity secondEntity = new SecondEntity(); > secondEntity.setSecondaryString("Secondary String"); > secondEntity.setSecondaryDate(new Date()); > > InnerEntity innerEntity = new InnerEntity(); > innerEntity.setInnerString("Inner String"); > innerEntity.setInnerDate(new Date()); > > SomeEntity someEntity = new SomeEntity(); > someEntity.setInnerEntity(innerEntity); > someEntity.setSecondEntity(secondEntity); > someEntity.setSomeDate(new Date()); > someEntity.setSomeString("Some string"); > > x.toXML(someEntity) > > SomeEntity: > > > > @XStreamAlias("SomeEntity") > public class SomeEntity { > @XStreamAlias("innerEntity") > private InnerEntity innerEntity; > @XStreamAlias("secondEntity") > private SecondEntity secondEntity; > @XStreamAlias("someString") > private String someString; > @XStreamAlias("someDate") > private Date someDate; > > public InnerEntity getInnerEntity() { > return innerEntity; > } > > public void setInnerEntity(InnerEntity innerEntity) { > this.innerEntity = innerEntity; > } > > public SecondEntity getSecondEntity() { > return secondEntity; > } > > public void setSecondEntity(SecondEntity secondEntity) { > this.secondEntity = secondEntity; > } > > public String getSomeString() { > return someString; > } > > public void setSomeString(String someString) { > this.someString = someString; > } > > public Date getSomeDate() { > return someDate; > } > > public void setSomeDate(Date someDate) { > this.someDate = someDate; > } > } > > InnerEntity: > > @XStreamAlias("InnerEntity") > public class InnerEntity { > @XStreamAlias("innerString") > private String innerString; > @XStreamAlias("innerDate") > private Date innerDate; > > public String getInnerString() { > return innerString; > } > > public void setInnerString(String innerString) { > this.innerString = innerString; > } > > public Date getInnerDate() { > return innerDate; > } > > public void setInnerDate(Date innerDate) { > this.innerDate = innerDate; > } > } > > SecondEntity: > > @XStreamAlias("SecondEntity") > public class SecondEntity { > @XStreamAlias("secondaryString") > private String secondaryString; > @XStreamAlias("secondaryDate") > private Date secondaryDate; > > public String getSecondaryString() { > return secondaryString; > } > > public void setSecondaryString(String secondaryString) { > this.secondaryString = secondaryString; > } > > public Date getSecondaryDate() { > return secondaryDate; > } > > public void setSecondaryDate(Date secondaryDate) { > this.secondaryDate = secondaryDate; > } > } > > > I get > > <SomeEntity> > <innerEntity> > <innerDate>2013-02-28 18:04:24.184 UTC</innerDate> > <innerString>Inner String</innerString> > </innerEntity> > <secondEntity> > <secondaryDate>2013-02-28 18:04:24.183 UTC</secondaryDate> > <secondaryString>Secondary String</secondaryString> > </secondEntity> > <someDate>2013-02-28 18:04:24.184 UTC</someDate> > <someString>Some string</someString> > </SomeEntity> > > When I need: > > <SomeEntity> > <innerEntity> > <innerString>Inner String</innerString> > <innerDate>2013-02-28 18:04:24.184 UTC</innerDate> > </innerEntity> > <secondEntity> > <secondaryString>Secondary String</secondaryString> > <secondaryDate>2013-02-28 18:04:24.183 UTC</secondaryDate> > </secondEntity> > <someDate>2013-02-28 18:04:24.184 UTC</someDate> > <someString>Some string</someString> > </SomeEntity> XStream itself does no sorting. If your class type is handled by the reflection provider, it simply requests the declared fields of a type using reflection and handles them in the sequence they are reported by the Java runtime. Now, this sequence is not defined by the runtime itself, but by the compiler used to compile a class. Normally the fields get created (and reported) in the declaration sequence. Personally I know only two compilers that created a different order: Compilers of the IBM JDK in old versions of Java 1.4.2 and 1.3.x and the compiler that was delivered once with Apache Harmony. Since I assume that you used the same compiler for all your classes and they have not been modified using byte-code manipulation libraries like ASM, CGLIB, BCEL or Javaassist, it is a bit strange that for one of you classes the reported sequence does not match the declaration order ... Nevertheless, you can influence the field sequence or sorting using a FieldKeySorter. Have a look at the CustomFieldKeySorterTest in XStream's acceptance package. However, if the SecondEntity is the only type where this happens and if the type is really that simple, you ma get away a lot easier by simply writing a custom converter for it. Have a look at the convertere tutorial, is is easy and straight forward. Cheers, Jörg --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email
