ZYJ
ZYJ

Reputation: 57

JAXB and property ordering with list of elements

I am using JAXB to unmarshall xml file. here is my code for the element feature, but I wanna have a special order of the elements in the element feature, like this

<feature>
  <name>2D Polynomial Approximation of Log of ConstantQ</name>
  <active>false</active>
  <attribute>50</attribute>
  <attribute>20</attribute>
  <attribute>10</attribute>
  <attribute>10</attribute>
</feature>

I checked some tutorial of @XmlType(propOrder = {}), but I cannot find a way to order with list of elements like the attribute elements here.

here is my codes

@XmlRootElement(name = "feature")
@XmlType(propOrder = {"name", "active","attribute"})
public class Feature{

    String name;

    boolean active;

    List<String> attributes = new LinkedList<String>();

    /**
     * name element of feature element
     * @return
     */
    @XmlElement(name = "name")
    public final String getName(){
        return this.name;
    }

    public final void setName(String name){
        this.name = name;
    }

    /**
     * active element
     * @return
     */
    @XmlElement(name = "active")
    public final boolean getActive(){
        return this.active;
    }

    public final void setActive(boolean active){
        this.active = active;
    }

    /**
     * attribute elements
     * @return
     */
    @XmlElement(name = "attribute")
    public final List<String> getAttributes(){
        return this.attributes;
    }

    public final void setAttributes(List<String> attributes){
        this.attributes = attributes;
    }
}

It always throw out exception, since I only define one attribute in propOrder. But since the attribute is multiple, could be one or more, I do not have any idea to implement it. Or do you know some other way to order the elements

Thanks for your help in advance

Upvotes: 3

Views: 2255

Answers (1)

bdoughan
bdoughan

Reputation: 149057

The propOrder is based on the field/property name and not the XML element name. So you should have

@XmlType(propOrder = {"name", "active","attributes"})

For More Information

Upvotes: 3

Related Questions