I have been trying to do this for 2 days now and I am about to pull my hair out. I want to make a class that can serialize/deserialize a list of objects of type T into XML using JDK7 and JAXB. I made a class that contains 2 methods to do this:
The saveToFile method contains the following code:
The JaxbList is just a wrapper for a list, since it seems that JAXB cannot save a variable of type List<T> directly.
When I first call saveToFile method and then loadFromFile method the list contains null. I think that is because JAXB cannot determine the type of the variable in the list. But I think it should be able to determine it when I can get the saveToFile method to create a better XSD schema. As you can see below, the list now contains where I think type="xs:anyType" should be type="rssItem" .
I tried to use the @XmlElement (type=... annotation in the JaxbList but I cannot use it since the type is of type T. The class is to be a library class that I plan to use in multiple projects, so it cannot have knowledge of the type of object it saves other than the object is of type T. See also the complete code (this forum does not allow attachments?) at http://pastebin.com/xh0CwUtt
All ideas are welcome because I am about to smash my head on my keyboara ljfk; .m?":
Gijs van Wieringen
Joined: May 22, 2005
The loadFromFile method works fine when I edit the schema and change type="xs:anyType" to type="rssItem". So the question rather is: how do I get JAXB to show the correct type in the generated schema.
In the saveToFile method I could open the generated schema afterwards and replace xs:anyType into rssItem but that's not a very elegant solution. There has got to be a better way..
Have you considered using, for instance, XStream (http://xstream.codehaus.org/) which will allow you to serialize almost any kind of object to XML and vice versa, without mapping?
Using JAXB without first creating a number of JAXB bean classes seems slightly awkward to me and I am afraid that you work too hard.
The problem miraculously solved itself! I made no changes to the code I posted and today everything works. Now I am confused as to what the problem was . Hallucinations, lack of sleep, a corrupt intellij cache or not enough computer reboots? Who knows. In contrast to what I thought and wrote earlier, JAXB actually is able to determine the type of the variable in the list automatically by itself when it tries to unmarshall a list that is described in the XSD of type xs:anyType.
XStream might have been an easier choice, but I wanted to learn about JAXB because JAXB is new to me and I already did some experimenting with XStream In any case, thank you for your reply!
For those curious how I did it, here is the complete class
Joined: Oct 04, 2006
Some times the compiled classes in a project in an IDE become stale without the IDE recompiling them. Perhaps something like that happened to you.
In Eclipse, one can use Clean Project. In NetBeans, there is Clean and Rebuild.
Thanks for sharing your experiences!