Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmarshalexception unexpected element, Expected elements are (none)

Tags:

java

xml

jaxb

I have problem when I want to deserialaze JAXBElement which I already serialized from byte array. I got 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)

here is my code:

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 is generated class from 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;
    }

}
like image 354
hudi Avatar asked Mar 24 '15 09:03

hudi


People also ask

What is UnmarshalException?

public class UnmarshalException extends JAXBException. This exception indicates that an error has occurred while performing an unmarshal operation that prevents the JAXB Provider from completing the operation. The ValidationEventHandler can cause this exception to be thrown during the unmarshal operations.

What is @XmlRootElement?

When a top level class or an enum type is annotated with the @XmlRootElement annotation, then its value is represented as XML element in an XML document. This annotation can be used with the following annotations: XmlType , XmlEnum , XmlAccessorType , XmlAccessorOrder .

What is JAXB context?

The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations: unmarshal, marshal and validate.


2 Answers

You should have a class named ObjectFactory beside your class AvizoType. If so then try this approach:

final JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
return ((JAXBElement<AvizoType>) jaxbContext.createUnmarshaller().unmarshal(
                inputStream)).getValue();
like image 131
thomas.mc.work Avatar answered Sep 20 '22 04:09

thomas.mc.work


A element can either be associated with a class with an @XmlRootElement or @XmlElementDecl annotation. If it's the latter you need to ensure the generated ObjectFactory is part of the classes used to bootstrap the JAXBContext. For models generated from an XML Schema you should create the JAXBContext on the generated package name(s) or ObjectFactory classes.

like image 30
bdoughan Avatar answered Sep 21 '22 04:09

bdoughan