Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

JAXB Question

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 56
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic