Lars Ladegaard
Lars Ladegaard

Reputation: 116

UnmarshalException: unexpected element on XML just been Marshaller

I get an UnmarshalException (unexpected element) when unmarshallering XML, which i have just run through marshaller. I looks like the marshalling process generates XML, which cannot be unmarshalled.

ObjectFactory objectFactory = new ObjectFactory();
EjendomSoegType type = objectFactory.createEjendomSoegType();
EjendomSoegningKriterierType krit = new EjendomSoegningKriterierType();
{
    krit.setBy("Skovlunde");
}
type.setEjendomSoegningKriterier(krit);
JAXBElement<EjendomSoegType> soeg = objectFactory.createEjendomSoeg(type);

// create JAXBContext which will be used to update writer
JAXBContext context = JAXBContext.newInstance(EjendomSoegType.class);

// marshall or convert jaxbElement containing element to xml format
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(soeg, writer);

String xml = writer.toString();
System.out.println( xml );

Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader reader = new StringReader(xml);
soeg = (JAXBElement<EjendomSoegType>) unmarshaller.unmarshal(reader);

The following UnmarshalException is thrown by the last line of the code unmarshaller.unmarshal(reader):

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://rep.oio.dk/tinglysning.dk/service/message/elektroniskakt/1/", local:"EjendomSoeg"). Expected elements are (none)
     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:609)
     at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:244)

The generated XML looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns8:EjendomSoeg xmlns:ns2="http://rep.oio.dk/tinglysning.dk/schema/model/1/" 
        xmlns:ns4="http://rep.oio.dk/cpr.dk/xml/schemas/core/2005/03/18/" 
        xmlns:ns3="http://rep.oio.dk/kms.dk/xml/schemas/2005/03/11/" 
        xmlns:ns5="http://rep.oio.dk/bbr.dk/xml/schemas/2005/03/11/" 
        xmlns:ns6="http://rep.oio.dk/bbr.dk/xml/schemas/2005/12/15/" 
        xmlns:ns7="http://rep.oio.dk/tinglysning.dk/schema/elektroniskakt/1/" 
        xmlns:ns8="http://rep.oio.dk/tinglysning.dk/service/message/elektroniskakt/1/">
    <ns7:EjendomSoegningKriterier>
        <By>Skovlunde</By>
    </ns7:EjendomSoegningKriterier>
</ns8:EjendomSoeg>

Why is the UnmarshalException thrown?

ADDITIONAL INFORMATION ADDED LATER: The createEjendomSoeg method in ObjectFactory has the @XmlElementDecl tag.

/**
 * Create an instance of {@link JAXBElement }{@code <}{@link EjendomSoegType }{@code >}}
 * 
 */
@XmlElementDecl(namespace = "http://rep.oio.dk/tinglysning.dk/service/message/elektroniskakt/1/", name = "EjendomSoeg")
public JAXBElement<EjendomSoegType> createEjendomSoeg(EjendomSoegType value) {
    return new JAXBElement<EjendomSoegType>(_EjendomSoeg_QNAME, EjendomSoegType.class, null, value);
}

Upvotes: 1

Views: 975

Answers (1)

bdoughan
bdoughan

Reputation: 148977

The @XmlElementDecl annotations on the ObjectFactory are not being picked up. To have ObjectFactory processesed you need to create the JAXBContext on this class, or on the package of your generated model.

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

If there isn't an @XmlRootElement or @XmlElementDecl corresponding to the element you are trying to unmarshal then you will need to use an unmarshal method that takes a Class parameter.

soeg = unmarshaller.unmarshal(new StreamSource(reader), EjendomSoegType.class);

Upvotes: 2

Related Questions