File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes JAXB Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "JAXB Question" Watch "JAXB Question" New topic
Author

JAXB Question

Andy Holm
Ranch Hand

Joined: Jul 03, 2008
Posts: 86
I have XML like the following:



I have a Java Bean like the following:



I would like to parse all of the values into one Java class using JAXB. How do I do that without having to create inner classes for the header and body portions of the XML?

Ramon Anger
Ranch Hand

Joined: Apr 19, 2011
Posts: 56

Hi Andy,

I would do the following:

create a Header class


create a Body class


create a message class


and test JAXB usage as follows:


There are no inner classes and you parse your xml against your message class.

Cheers,
Ramon


Blackbelt on BlackBeltFactory.com.
Andy Holm
Ranch Hand

Joined: Jul 03, 2008
Posts: 86
Having separate classes defeats what I am trying to accomplish. I am trying to combine all elements into a single DTO that can be passed around the business layer of an application without having to know the structure of the XML source. Is there any way to map specific field values of a single bean to different levels of an XML document. For instance, can JavaBean.id be mapped to XML tag MESSAGE/HEADER/ID directly without creating separate classes or inner classes to maintain the XML structure?
Ramon Anger
Ranch Hand

Joined: Apr 19, 2011
Posts: 56

Hi Andy,

without additional classes I see no way. But with a little bit more coding you can reach your goal.

add the four message attributes to class message (without XmlElement annotation)


and copy the values from your body and header attributes after unmarshalling into these attributes.


The resulting structure is as flat as you showed in your initial post. One problem is that you have two additional attributes body and header. From my point of view this is no problem.

Cheers,
Ramon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAXB Question