Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm fairly new to JAXB and am eager to learn more about it. What I've noticed is that when marshalling, the XML representation of the objects from an array and list are the identical.

What I'm interested in finding out is how JAXB treats the two data structures when un/marshalling and if it's better to standardize it to use one over the other? If so, what is the difference between the two (performance-wise, etc.)? Also, what should I consider when choosing a container for my objects?

Any information will be appreciated. Thank you!

like image 386
Hal Li Avatar asked Jan 06 '15 21:01

Hal Li


People also ask

What can I use instead of JAXB?

XOM, JDOM, dom4j, etc. etc. Projects like Castor and Apache XMLBeans predate JAXB, so you could have a look at those. Ulf Dittmer wrote: XOM, JDOM, dom4j, etc.

What is marshalling JAXB?

The JAXB Marshaller interface is responsible for governing the process of serializing Java content trees i.e. Java objects to XML data. This marshalling to XML can be done to variety of output targets.

What is Unmarshalling Java?

The Unmarshaller class governs the process of deserializing XML data into newly created Java content trees, optionally validating the XML data as it is unmarshalled. It provides an overloading of unmarshal methods for many different input kinds.


1 Answers

In XML representation both arrays and Lists have the same form. When unmarshaling an XML, JAXB will choose the type you have in your Java class.

It's possible to unmarshal a collection to an array which was marshalled from a List and vice versa.

Both arrays and Lists have their pros and cons. Use what is better for your purpose. In general List are easier to use because you have utility methods like List.contains(), but Lists cannot be used for primitive types. Arrays can be faster but are less flexible.

It's really up to you which you use and which is better for you.

like image 150
icza Avatar answered Sep 28 '22 10:09

icza