my dog learned polymorphism*
The moose likes Web Services and the fly likes JAXB Saving and loading a generic List of type T to/from XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "JAXB Saving and loading a generic List of type T to/from XML" Watch "JAXB Saving and loading a generic List of type T to/from XML" New topic
Author

JAXB Saving and loading a generic List of type T to/from XML

Gijs van Wieringen
Greenhorn

Joined: May 22, 2005
Posts: 10
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
Greenhorn

Joined: May 22, 2005
Posts: 10
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..
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
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.
Best wishes!


My free books and tutorials: http://www.slideshare.net/krizsan
Gijs van Wieringen
Greenhorn

Joined: May 22, 2005
Posts: 10
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

Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
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!
 
 
subject: JAXB Saving and loading a generic List of type T to/from XML
 
Similar Threads
soap fault return empty <detail> element.
creating xml file with xml schema in java
Creating a JMS TextMessage From XML
.xsd schema questions
returning a 2-d array