java - JAXB: multiple tags of the same kind in a specific order -


i have rather unusual xml-format need marshall , unmarshall:

<a>   <b></b>   <c></c>   <d></d>   <c></c>   <d></d> </a> 

the code expecting work didn't is:

@xmlaccessortype(xmlaccesstype.none) @xmltype(proporder={"b", "elist"}) @xmlrootelement(name="a") public class {      @xmlelement(name="b")     private string b;      @xmlelementrefs({         @xmlelementref(name="c", type=string.class),         @xmlelementref(name="d", type=string.class)     })     @xmlmixed     private list<string> elist; } 

the result sadly missing correct order (i need b,c,d,c,d order):

<a>   <b></b>   <c></c>   <c></c>   <d></d>   <d></d> </a> 

i tried different things @xmlmixed, sub-objects @xmlpath nothing worked me. hints or links? in advance!

i suggest below solution

@xmlaccessortype(xmlaccesstype.field) @xmltype(name = "a", proporder = {     "bs",     "cs",     "ds" }) @xmlrootelement(name = "a") public class     implements serializable {      private final static long serialversionuid = 1234567890l;     @xmlelement(name = "b")     protected list<string> bs;     @xmlelement(name = "c")     protected list<string> cs;     @xmlelement(name = "d")     protected list<string> ds;      public list<string> getbs() {         if (bs == null) {             bs = new arraylist<string>();         }         return this.bs;     }      public list<string> getcs() {         if (cs == null) {             cs = new arraylist<string>();         }         return this.cs;     }       public list<string> getds() {         if (ds == null) {             ds = new arraylist<string>();         }         return this.ds;     }  } 

and might apply xsd validation.

<?xml version="1.0" encoding="utf-8"?> <xs:schema elementformdefault="qualified"     targetnamespace="http://yournamespace" xmlns="http://yournamespace" xmlns:xs="http://www.w3.org/2001/xmlschema">     <xs:element name="a" type="a" />     <xs:complextype name="a">         <xs:sequence>             <xs:element name="b" type="xs:string" minoccurs="0" maxoccurs="unbounded" />             <xs:element name="c" type="xs:string" minoccurs="0" maxoccurs="unbounded" />             <xs:element name="d" type="xs:string" minoccurs="0" maxoccurs="unbounded" />         </xs:sequence>     </xs:complextype>  </xs:schema> 

Comments