我已经使用sqlite和Java创建了一个数据库,我想将成功插入到名为“ BazaDeDate”的数据库中的数据导出到XML File,但是它不起作用,这给了我这个错误。这是代码:

public class ExportXML {

    public static void main(String [] args) throws Exception
    {
        Class.forName("org.sqlite.JDBC");
        Connection conexiune=DriverManager.getConnection("jdbc:sqlite:BazaDeDate.db");
        Statement comanda=conexiune.createStatement();

        ResultSet rs=comanda.executeQuery("SELECT* FROM AvionPasageri");


        JAXBContext context=JAXBContext.newInstance(AvionPasageri.class);

        Marshaller marshaller=context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);

        while(rs.next())
        {
            String serie=rs.getString("serie");
            float tonaj=rs.getFloat("tonaj");
            String marca=rs.getString("marca");
            float capacitate=rs.getFloat("capacitate");
            String cnp=rs.getString("cnp");
            List<String>listaCnp=new Vector<>();
            listaCnp.add(cnp);

            AvionPasageri ap=new AvionPasageri(serie, tonaj, marca, capacitate,listaCnp);

            File f=new File("AvionPasageri.xml");
            marshaller.marshal(ap,f);

        }



    }
}


AvionPasageri是我的类,用于创建插入到数据库中的对象,现在我想导出为XML。

serie,tonaj,marca,capacitate,cnp用于引用类AvionPasageri的属性。

请帮助我找出问题所在。

这些是错误:

Exception in thread "main" javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "pachetClase.AvionPasageri" as an element because it is missing an @XmlRootElement annotation]
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
    at pachetExportXML.ExportXML.main(ExportXML.java:44)
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "pachetClase.AvionPasageri" as an element because it is missing an @XmlRootElement annotation
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source)
    ... 4 more


编辑:类AvionPasageri,派生自抽象类Avion看起来像这样:

public class AvionPasageri extends Avion implements Cloneable {

    private float nrLocuri;
    private List<String>cnpPasageri=new Vector<String>();

    public AvionPasageri(String serie, float tonaj, String marca, float nrLocuri, List<String>cnpPasageri) throws Exception
    {
        super(serie, tonaj, marca);
        if(tonaj<0)
            throw new Exception("Tonaj negativ!");

        this.setNrLocuri(nrLocuri);
        this.setCnpPasageri(cnpPasageri);

    }

    //properties(get&set for attributes) are here

    @Override
    public float getCapacitate() {...} //abstract method inherited

    @Override
    public AvionPasageri clone() throws CloneNotSupportedException {..}

    @Override
    public boolean equals(Object obj) {...}

    @Override
    public String toString() {...}

}


我在某些方法的主体中添加了“ ...”以使代码更短。

最佳答案

如前所述,您可以在类的开头包含@XmlRootElement。但是对于我来说,这些类是自动生成的,因此对我来说不是解决方案。如果要封送没有标签的对象,只需包含QName并生成没有@XmlRootElement的对象:

        // Here include your package and your class
        QName qName = new QName("com.atos.nextcare.orm", "ORMO01CONTENT");
        JAXBElement<ORMO01CONTENT> root = new JAXBElement<>(qName, ORMO01CONTENT.class, request.getORMO01());

        StringWriter sw = new StringWriter();
        marshaller.marshal(root, sw);


我按照给出的示例here

07-27 18:01