File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes JAXB Marshalling Object-Array Nesting Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "JAXB Marshalling Object-Array Nesting Issue" Watch "JAXB Marshalling Object-Array Nesting Issue" New topic

JAXB Marshalling Object-Array Nesting Issue

Perry Terrance
Ranch Hand

Joined: Oct 02, 2009
Posts: 62
This has to be very simple, but I can't figure out whats going on.

I have a simple JAXB POJO myPOJO - which only has one field of an array of Objects - myObjects which has two instances of Object innerObject in it. The innerObject has a single String Field of "test".

So it goes like this:

myPOJO -> [] myObjects array-> array of [innerObject("test"), innerObject("test")]

When I marshal this Java to XML - I expect to get the following structure:

instead I get this:

Why is my innerObject array nested like this and where is innerObject's own XML element-name? Why are there 2 XML-elements of myObjects when it is a single Object array?

I have only one XML JAXB annotation of @XmlRootElement at the top-level myPOJO class...

g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 554

I have only one XML JAXB annotation of @XmlRootElement at the top-level class...

In that case, you're using default XmlAccessorType. The minimum you can do is to add these two annotations to your public getter (the property) on List<MyObject> like this.
I agree. Here's the link:
subject: JAXB Marshalling Object-Array Nesting Issue