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.
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.
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.
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.
Joined: May 02, 2007
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: