JAXB un / marshalling: is there a difference between using arrays and lists?

I am new to JAXB and really want to know more about this. I noticed that when sorting, the XML representation of the objects from the array and the list is identical.

What Iโ€™m interested to know is how JAXB treats two data structures when un / marshalling and, if it is better to standardize it to use one over the other? If so, what is the difference between them (in terms of performance, etc.)? In addition, what should I consider when choosing a container for my objects?

Any information would be appreciated. Thank!

+4
source share
3 answers

XML List . XML JAXB , Java.

, List .

List . , . List , , List.contains(), List . , .

, .

+2

, :

  • JAXB, , SAX StAX XML. , XML, List , .
  • JAXB impls , .
+2

There is no difference between a list or an array for sorting or disassembling. It can work in both senses, when you marshal an array / List, you can unmount the list / array.

There is a simple example:

@XmlRootElement(name = "ROOT")
@XmlAccessorType(XmlAccessType.FIELD)
public class JAXBExample {

    @XmlElement(name = "list")
    private List<Elm> list;
    @XmlElement(name = "array")
    private Elm[] array;

    public JAXBExample() {
        this.list = new ArrayList<>();
        this.array = new Elm[0];
    }

    public void addList(Elm elm) {
        this.list.add(elm);
    }

    public void addArray(Elm elm) {
        Elm[] tmp = new Elm[array.length+1];
        System.arraycopy(array, 0, tmp, 0, array.length);
        tmp[array.length] = elm;
        this.array = tmp;
    }

    private void clear() {
        this.array = null;
        this.list.clear();
    }

    @XmlRootElement(name = "element")
    @XmlAccessorType(XmlAccessType.FIELD)
    static class Elm {

        @XmlElement(name = "name")
        private final String name;

        public Elm() {
            this.name = "noname";
        }

        public Elm(String name) {
            this.name = name;
        }
    }

    public static void main(String... arg) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(JAXBExample.class,
            Elm.class);
        Marshaller marshaller = context.createMarshaller();
        Unmarshaller unmarshaller = context.createUnmarshaller();

        JAXBExample jaxb = new JAXBExample();
        jaxb.addArray(new Elm("Elm1"));
        jaxb.addArray(new Elm("Elm3"));
        jaxb.addArray(new Elm("Elm5"));
        jaxb.addArray(new Elm("Elm7"));
        jaxb.addArray(new Elm("Elm9"));
        jaxb.addList(new Elm("Elm2"));
        jaxb.addList(new Elm("Elm4"));
        jaxb.addList(new Elm("Elm6"));
        jaxb.addList(new Elm("Elm8"));
        jaxb.addList(new Elm("Elm10"));

        File f = new File("example.xml");
        marshaller.marshal(jaxb, f);

        jaxb.clear();
        jaxb = null;

        jaxb = (JAXBExample) unmarshaller.unmarshal(f);
        for (Elm elm : jaxb.array) {
            System.out.println("ArrayElm: " + elm.name);
        }
        for (Elm elm : jaxb.list) {
            System.out.println("ListElm: " + elm.name);
        }

    }
}

It produces:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ROOT>
    <list>
        <name>Elm2</name>
    </list>
    <list>
        <name>Elm4</name>
    </list>
    <list>
        <name>Elm6</name>
    </list>
    <list>
        <name>Elm8</name>
    </list>
    <list>
        <name>Elm10</name>
    </list>
    <array>
        <name>Elm1</name>
    </array>
    <array>
        <name>Elm3</name>
    </array>
    <array>
        <name>Elm5</name>
    </array>
    <array>
        <name>Elm7</name>
    </array>
    <array>
        <name>Elm9</name>
    </array>
</ROOT>

As you can see, both arrays and lists have the same storage format.

0
source

Source: https://habr.com/ru/post/1570217/


All Articles