// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;

public class MultiSer {
    public static void main(String[] args) throws IOException, ClassNotFoundException, JAXBException {
        List<Data> lst = new ArrayList<Data>();
        lst.add(new Data(1, "A"));
        lst.add(new Data(2, "BB"));
        lst.add(new Data(3, "CCC"));
        // binary
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\bin.dat"));
        oos.writeObject(lst);
        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\bin.dat"));
        List<Data> binlst = (List<Data>)ois.readObject();
        ois.close();
        for(Data d : binlst) {
            System.out.println(d);
        }
        // old XML
        XMLEncoder xe = new XMLEncoder(new FileOutputStream("C:\\xmlencdec.xml"));
        xe.writeObject(lst);
        xe.close();
        XMLDecoder xd = new XMLDecoder(new FileInputStream("C:\\xmlencdec.xml"));
        List<Data> oldxmllst = (List<Data>)xd.readObject();
        xd.close();
        for(Data d : oldxmllst) {
            System.out.println(d);
        }
        // new XML
        JAXBContext jxbctx = JAXBContext.newInstance(Wrapper.class);
        OutputStream os = new FileOutputStream("C:\\xmljaxb.xml");
        Marshaller m = jxbctx.createMarshaller();
        m.marshal(new Wrapper(lst), os);
        os.close();
        InputStream is = new FileInputStream("C:\\xmljaxb.xml");
        Unmarshaller um = jxbctx.createUnmarshaller();
        List<Data> newxmllst = ((Wrapper)um.unmarshal(is)).getLst();
        is.close();
        for(Data d : oldxmllst) {
            System.out.println(d);
        }
    }
}

@XmlRootElement
class Wrapper {
    private List<Data> lst;
    public Wrapper() {
        this(null);
    }
    public Wrapper(List<Data> lst) {
        this.lst = lst;
    }
    public List<Data> getLst() {
        return lst;
    }
    public void setLst(List<Data> lst) {
        this.lst = lst;
    }
}
