0

I have two classes:

@XmlSeeAlso(ListType.class)
public class Type {
    private int id;

    public int getId() {
        return id;
    }
    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }
}

@XmlRootElement
public class ListType extends Type {

    private String name;

    private String namespace;

    public String getName() {
        return name;
    }

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

    public String getNamespace() {
        return namespace;
    }

    @XmlAttribute
    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }
}

And I make marshalling:

ListType listType = new ListType();
listType.setId(123);
listType.setName("Name");
listType.setNamespace("Namespace");

JAXBContext jaxbContext = JAXBContext.newInstance(UserType.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

jaxbMarshaller.marshal(listType, System.out);

Finally, I get XML like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<listUserType name="Name" namespace="Namespace" id="123"/>

All is well, just tell me, how can I specify the order of the attributes in my XML?

I want them to go in this order:

<listType id="123" name="Name" namespace="Namespace" />

This is very necessary for solving my task. Thank you

All_Safe
  • 1,339
  • 2
  • 23
  • 43
  • If it’s necessary for your task, you’re not parsing XML correctly. Attribute order is meaningless. [The XML specification](https://www.w3.org/TR/xml/#sec-starttags) states “Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.” – VGR Dec 20 '17 at 17:59

1 Answers1

2

Annotate the parent class with @XmlTransient, it will help you to include the properties from parent class then

add this @XmlType(propOrder={"Id", "Name", "Namespace"}) on top of ListUserType class.

Pmakari
  • 264
  • 5
  • 13