3

I have nothing more complex than a list of strings. I have implemented several methods to make simpler use of JAXB to serialize various classes.

The List exists inside no other classes, it's just a simple List that I need to load into a ComboBox at run time, and allow the user to save/load the contents of at their leisure.

Is it possible for me to use JAXB to serialize a List object, and if so, how?

Will
  • 3,413
  • 7
  • 50
  • 107
  • Yes, it's possible. If the list is referenced by a field in a serialized class, you can annotate it with @XmlElement. See also this possible duplicate: http://stackoverflow.com/questions/1603404/using-jaxb-to-unmarshal-marshal-a-liststring – Andy Thomas Oct 28 '14 at 18:12

2 Answers2

2

It is possible, but it will be less efficient than a simple wrapper class with a List field.

Following your comments on @BlaiseDoughan's answer, here is your SaveableList implementation. Basically what you need is a class which implements List and forwards all calls to an internal List implementation, e.g. ArrayList. In my example I extend AbstractList for simplicity.

SaveableList implementation:

class SaveableList extends AbstractList<String> {

    @XmlElement(name="e")
    private final List<String> list = new ArrayList<>();

    @Override
    public String get(int index) {
        return list.get(index);
    }

    @Override
    public boolean add(String e) {
        return list.add(e);
    }

    @Override
    public int size() {
        return list.size();
    }

    // You might want to implement/override other methods which you use
    // And is not provided by AbstractList

}

Example usage:

List<String> list = new SaveableList();
list.add("one");
list.add("two");

// Save it to XML
JAXB.marshal(list, new File("list.xml"));

// Load it from XML:
List<String> list2 = JAXB.unmarshal(new File("list.xml"), SaveableList.class);

Here is the XML it generates:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<saveableList>
    <e>one</e>
    <e>two</e>
</saveableList>
icza
  • 389,944
  • 63
  • 907
  • 827
0

JAXB requires the object you are marshalling to be a domain object. Therefore you would need to introduce a wrapper like the following to do the marshalling.

 @XmlRootElement(name="strings)
 public class ListOfStrings {

     private List<String> strings;

     @XmlElement(name="string")
     public List<String> getStrings() {
         return strings;
     }

     public void setStrings(List<String> strings) {
         this.strings = strings;
     }

 }
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • That's of what I was afraid. I was really hoping to avoid that... Oh well. Is it possible to implement the List<> class into another class and use that? – Will Oct 28 '14 at 18:14
  • @Will I'm not sure what you mean by `Is it possible to implement the List<> class into another class and use that?`? – bdoughan Oct 28 '14 at 18:18
  • Well, could you do say the following: @XmlRootElement(name = "SaveableList") public abstract class SaveableList implements List{} – Will Oct 28 '14 at 18:23