• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JAXB syntax help

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

Data.java




Table.java



Bracket.java

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

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



 
Fire me boy! Cool, soothing, shameless self promotion:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic