File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAXB syntax help

 
Matthew Snow
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use @XmlAttribute for attributes and @XmlElement for elements. You do not need to create a name class.

 
Matthew Snow
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that worked.
 
Matthew Snow
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kind of the same thing:

Data.java




Table.java



Bracket.java

 
Matthew Snow
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Matthew Snow
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, but I've got another. Back to the Pet XML, here's what I got:

 
g tsuji
Ranch Hand
Posts: 650
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic