Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAXB Question

 
Andy Holm
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andy Holm
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic