com.sun.istack.internal.SAXException2:无法将类型编组为元素

时间:2017-07-01 15:40:05

标签: java xml sqlite

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

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是我的类,用于创建我插入数据库的对象,现在我想导出到XML。

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

请帮我弄清楚是什么问题。

这些是错误:

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() {...}

}

编辑:AvionPasageri类,源自抽象类Avion,如下所示:

<android.support.v7.widget.Toolbar
    android:id="@+id/tb_title_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textAllCaps="true"
        android:textColor="#2B2B2B"
        android:textSize="14sp" />

</android.support.v7.widget.Toolbar>

我在一些方法的主体中放了一些“...”来缩短代码。

2 个答案:

答案 0 :(得分:1)

如前所述,您可以在类的开头包含@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

答案 1 :(得分:0)

要使JAXB正常工作,您必须在类和所需的XML元素之间提供映射。此映射以添加到类中的注释的形式提供,这些注释表示XML元素。您可以在网上找到有关如何操作的教程: http://www.vogella.com/tutorials/JAXB/article.html https://docs.oracle.com/javase/tutorial/jaxb/intro/

此外,如果您拥有所需xml的xsd架构,那么JAXB可以为您生成必要的类。