java - Unmarshalexception 意外元素,预期元素是(无)

标签 java xml jaxb

当我想反序列化已从字节数组序列化的 JAXBElement 时遇到问题。我有异常(exception):

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Avizo"). Expected elements are (none)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1048)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)

这是我的代码:

public class JAXBTest {

    public static byte[] serialize(JAXBElement<AvizoType> xmlData) throws Exception {
        // Result stream buffer
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        final JAXBContext jaxbContext = JAXBContext.newInstance(AvizoType.class);

        final Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(xmlData, new StreamResult(bos));

        return bos.toByteArray();
    }

    public static JAXBElement<AvizoType> deserialize(byte[] data) throws Exception {
        // Result stream buffer
        ByteArrayInputStream bis = new ByteArrayInputStream(data);

        final JAXBContext jaxbContext = JAXBContext.newInstance(AvizoType.class);

        final Unmarshaller marshaller = jaxbContext.createUnmarshaller();

        return (JAXBElement<AvizoType>) marshaller.unmarshal(bis);
    }

    public static void main(String[] args) throws Exception {
        AvizoType type = new AvizoType();
        type.setAvizoId(1);

        JAXBElement<AvizoType> element = new JAXBElement(new QName("Avizo"), AvizoType.class, type);

        byte[] result = serialize(element);
        deserialize(result);

    }
}

avizoType 是从 xsd 生成的类:

    //
    // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.10-b140310.1920 
    // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
    // Any modifications to this file will be lost upon recompilation of the source schema. 
    // Generated on: 2015.03.23 at 01:59:39 PM CET 
    //


    import java.io.Serializable;

    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;

    /**
     * <p>
     * Java class for AvizoType complex type.
     * 
     * <p>
     * The following schema fragment specifies the expected content contained within this class.
     * 
     * <pre>
     * &lt;complexType name="AvizoType">
     *   &lt;complexContent>
     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *       &lt;sequence>
     *         &lt;element name="AvizoId" type="{http://www.w3.org/2001/XMLSchema}long"/>
     *       &lt;/sequence>
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     * 
     * 
     */

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AvizoType", propOrder = { "avizoId" })
public class AvizoType implements Serializable {

    @XmlElement(name = "AvizoId")
    protected long avizoId;

    /**
     * Gets the value of the avizoId property.
     * 
     */
    public long getAvizoId() {
        return avizoId;
    }

    /**
     * Sets the value of the avizoId property.
     * 
     */
    public void setAvizoId(long value) {
        this.avizoId = value;
    }

}

最佳答案

您的类 AvizoType 旁边应该有一个名为 ObjectFactory 的类。如果是这样,请尝试这种方法:

final JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
return ((JAXBElement<AvizoType>) jaxbContext.createUnmarshaller().unmarshal(
                inputStream)).getValue();

关于java - Unmarshalexception 意外元素,预期元素是(无),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29229308/

相关文章:

java - 如何在当前日期上添加 30 天?

java - 普通 Java 套接字中没有密码套件

c# - 无法读取简单的 xml 文件

java - 如何使用 JAXB 注释向父标记的 XML 元素添加属性

java - P6Spy 不使用 HSQLDB 记录 hibernate 更新

java - 创建类路径资源 [jpaContext.xml] 中定义的名称为 'entityManagerFactory' 的 bean 时出错

xml - 从 Vantage Pro 2 气象站发布数据的数据格式是什么?

java - 从大型 XML 文件中删除不可打印和/或 unicode 字符

soap - 谁在使用XML Schema 1.1版本?哪些解析器版本支持它?等等

java - 如何向此 TextView 添加自定义字体?