0

I have the following code. I would like to be able to construct the object created in the main method from an XML file. I was thinking about a structure in the form <Main><CustomModel/></Main>. Is this possible with JAXB and how can I achieve something like this?

@XmlRootElement
public class Main {   
    private final Model model;

    public Main(Model model) {
        this.model = model;
    }

    public static void main(String[] args) {
        Main m = new Main(new CustomModel());
    }
}

interface Model {};

class CustomModel implements Model {
    @XMLElement String test = "1234";
};
  • Check the answers to http://stackoverflow.com/questions/4387296/jaxb-and-constructors – Hakan Serce Mar 05 '14 at 22:56
  • hmm.. this still looks quite impossible though... Because it would require a Builder and Adapter for each subclass of the Interface, and even then you're not able to let JAXB find out which implementation for the interface should be deserialized? – Jan-Willem Gmelig Meyling Mar 06 '14 at 14:38

1 Answers1

0

I switched to Simple XML Serialization, because they handle interfaces and constructor parameters for setting final variables differently. See http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#list . I'm still not sure if this is possible with JAXB.