*
The moose likes XML and Related Technologies and the fly likes mapping xml to java using jaxb Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "mapping xml to java using jaxb" Watch "mapping xml to java using jaxb" New topic
Author

mapping xml to java using jaxb

Sven Hupert Kirf
Greenhorn

Joined: Mar 06, 2009
Posts: 2
hi guys,

im new here, so firstly: hello :P

i have a problem related to xml processing.

i need to map these xml structure


to java objects.

the <device> tag can ouccur only once where there can be an arbitrary number of <sensor> and <actuator> blocks.

in my frist approach i tried to solve it with sax, but i quickly saw that it will end up relatively complex so i discarded this way of solving the problem.

then i read about jaxb and it seems to be exactly what i need. but after working through some how to´s and introduction i still can´t solve my problem appropriate. for example i get error meesages like "if the annotation @XMLattribut is used, the class can´t have a @XMLelement tag" and so on.
(i used the variant with annotation because of the lack of a schema)

can you help me to figure out what classes i need and wich structure they need to have? the goal should be that i have a device object whith a list of components as attribute which holds the sensos and actuator objects.

thanks in advance

ps: i´ve uploaded the classes whith which i already tried and failed to solve the problem :P linkedit: deleted)
pps: sorry for the bad englisch, as you can see from the xml i´m german...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

"buggy blue", welcome to JavaRanch. Please check your private messages regarding an important administrative matter.

Thank you.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So you are just starting with JAXB? Then I would suggest this: start with one of the JAXB tutorial examples. Next, make changes to it until you understand JAXB better. Finally, change it more until it's your requirement.

At least, that's the strategy I use for learning and applying new Java features.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

A thought just occurred to me; you might want to look into Apache Digester for an XML-to-Java-object converter which is a little less rigid than JAXB.
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

Another way to get started with JAXB is to use Trang to generate an XML schema from your sample XML, then run xjc against that schema. Even if the results aren't perfect, it should give you some ideas.
Sven Hupert Kirf
Greenhorn

Joined: Mar 06, 2009
Posts: 2
hi,

i managed to annotate my classes so that they can be easily marshalled and unmarshalled.
the only problem im still struggling with is that i can´t create the leading <janitor> and trailing </janitor> tag.

my generated xml looks like this:



i know that there is the @ElementWrap annotation but unfortunately i can´t use thise in my Device Class because of the @RootElement Annotation



any ideas?
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

As you've seen, @XmlElementWrapper isn't meant to do this. With JAXB, you'll need a separate class for <janitor> and <device>, even if your Janitor class is only used for serialization and parsing.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: mapping xml to java using jaxb
 
Similar Threads
javax.el.PropertyNotFoundException: Property 'logoutUser' not found on type org.ssc.webapp.UserLogin
Trying to bind inputText fields from an a4j:repeat tag to java code
using JAXB generated classes as domain objects
Facade design pattern in web services
my package doesnt contain ObjectFactory.class or jaxb.index