views:

209

answers:

1

How can I get jaxb to bind to my Vector? I cannot seem to get it to bind a Vector that contains generics as it complains that it cannot recognize my class "shape" or any of its subtypes.. "[javax.xml.bind.JAXBException: class shape.shape nor any of its super class is known to this context.]"?

import java.util.Vector;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "XVector")
public class XVector<shape> {

    private Vector<shape> q;

    public XVector() {}

    @XmlElement(name = "q")
    public Vector<shape> getVector() {
        return q;
    }

    public void setVector(Vector<shape> q) {
        this.q = q;
    }
}

I get the following errors:

javax.xml.bind.MarshalException
 - with linked exception:
[javax.xml.bind.JAXBException: class shape.Rectangle nor any of its super class is known to this context.]
        at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:317)
        at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:243)
        at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:75)

public void saveFile(File filename) {
    try {
        FileOutputStream fout = new FileOutputStream(filename);
        objs.setVector(objVec);
        JAXBContext context = JAXBContext.newInstance(XVector.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        marshaller.marshal(objs, fout);
        fout.close();
    } catch (JAXBException e) {
       e.printStackTrace ();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}
+1  A: 

You should include all required classes in the JAXBContext

JAXBContext context = JAXBContext.newInstance(XVector.class, shape.class);

(note: the convention dictates that Shape should be capitalized)

Bozho
Thank you and shame on me for not capitalizing my class. I also found this link useful as it cleaned up my xml: http://weblogs.java.net/blog/2005/09/30/using-jaxb-20s-xmljavatypeadapter