Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reordering XML tags

I am trying to implement something which is for writing back the content tree of java object to XML file (object marshalling) (I know there are a lot of APIs for doing that but its required from me), I want to let the user to reorder the tags as he/she wants, I know using annotation like what JAXB has may solve that, but I think using annotation may cause a lot of pain. it will be so helpful if any one could offer any good approach.

Thanks

like image 740
Roz Ann Avatar asked Jul 10 '26 20:07

Roz Ann


1 Answers

JAXB (JSR-222) implementations provide a couple different mechanisms for specifying the order of XML elements when then content is marshalled to XML. JAXB does not require the elements be in order when unmarshalling.

OPTION #1 - @XmlType(propOrder={"c","b", "a"})

The propOrder property on the @XmlType annotation allows you to specify an order.

Root

package forum11217734;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlType(propOrder={"c","b", "a"})
public class Root {

    private String a;
    private String b;
    private String c;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <c>Baz</c>
    <b>Bar</b>
    <a>Foo</a>
</root>

For More Information

  • http://blog.bdoughan.com/2012/02/jaxbs-xmltype-and-proporder.html

OPTION #2 - @XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)

You can also use the @XmlAccessorOrder annotation to specify that the properties should be marshalled in alphabetical order.

Root

package forum11217734;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public class Root {

    private String a;
    private String b;
    private String c;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <a>Foo</a>
    <b>Bar</b>
    <c>Baz</c>
</root>

DEMO CODE

The following demo code was used to produce the output for each of the options above.

package forum11217734;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Root root = new Root();
        root.setA("Foo");
        root.setB("Bar");
        root.setC("Baz");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}
like image 116
bdoughan Avatar answered Jul 13 '26 11:07

bdoughan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!