| Author |
JAXB syntax help
|
Matthew Snow
Ranch Hand
Joined: May 02, 2007
Posts: 71
|
|
I need to unmarshal a tricky xml stream and I need some help. Here is an example of what I'm running into:
A sample POJO for this xml would look like this:
The problem is how do I capture the <name> element? If I set it as an @XmlAttribute name, it comes up null. If I create a java class called Name and include it in Pet as a @XmlElement, it does get instantiated so I guess that's on the right track, but I still don't know how to capture the value 'Fido'. I tried creating a constructor that accepts a String value, but that didn't work. I also tried creating a @XmlAttribute variable in the Name class called name, but that didn't work either. Please help.
|
When you break the big rules, you get a lot of little rules.
|
 |
Bill Gorder
Bartender
Joined: Mar 07, 2010
Posts: 1282
|
|
Use @XmlAttribute for attributes and @XmlElement for elements. You do not need to create a name class.
|
[How To Ask Questions][Read before you PM me]
|
 |
Matthew Snow
Ranch Hand
Joined: May 02, 2007
Posts: 71
|
|
|
Thanks, that worked.
|
 |
Matthew Snow
Ranch Hand
Joined: May 02, 2007
Posts: 71
|
|
Here is another conundrum.
Here is the code I have in class Data:
In class Table is where I'm unsure. I have a @XmlElement private Bracket bracket; which, when I run the code, gets set to the last bracket value (the C bracket). I thought I would create a transient ArrayList<Bracket> variable which I would instantiate in the constructor and put brackets.add(bracket); in the setBracket() method. However, it seems that JAXB doesn't call the getter/setter methods when unmarshalling the XML stream even though these variables are private. As a result, the ArrayList<Bracket> brackets variable is empty at runtime while the bracket variable is set to the last bracket value in the XML.
|
 |
Bill Gorder
Bartender
Joined: Mar 07, 2010
Posts: 1282
|
|
Kind of the same thing:
Data.java
Table.java
Bracket.java
|
 |
Matthew Snow
Ranch Hand
Joined: May 02, 2007
Posts: 71
|
|
|
Thank you.
|
 |
Matthew Snow
Ranch Hand
Joined: May 02, 2007
Posts: 71
|
|
Thanks for the help, but I've got another. Back to the Pet XML, here's what I got:
|
 |
g tsuji
Ranch Hand
Joined: Jan 18, 2011
Posts: 357
|
|
This would do.
This kind of questions can go on no end. My cheat-sheet has this: make out a simple schema and use the jaxb code generator to generate the classes. That often is what fills the high percentage of the need. And the above is what I did.
ps You keep posting this kind of xml like type=dog. This is not it should appear in the xml.
|
 |
Matthew Snow
Ranch Hand
Joined: May 02, 2007
Posts: 71
|
|
Thanks g tsuji, that does work. For anyone wondering, codehaus has a nice maven jaxb plugin for generating code from an xml schema (http://www.altuure.com/2008/01/22/jaxb-quickstart-via-maven2/).
Unfortunately, my Pet bean extends another class and JAXB does not allow the use of @XmlValue on a class that derives another class. I guess I'm just going to have to rethink my code structure on this one.
|
 |
Matthew Snow
Ranch Hand
Joined: May 02, 2007
Posts: 71
|
|
Solved it, putting the @XmlValue element in the parent class has solved this instance as well and any other empty name element occurrences as well. Here is what the two classes look like:
|
 |
 |
|
|
subject: JAXB syntax help
|
|
|