简体   繁体   中英

Unmarshalling jaxb fail on sub-elements (QName)

I am writting an unmarshaller and I am fighting hard with jaxb to have a working solution.

I wanna unmarshall a MessageDescription generated from.wsdl files. The java class from the wsdl file is:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MessageDescription", propOrder = {
    "source",
    "key",
    "data",
    "extension"
})
@XmlSeeAlso({
    org.onvif.ver10.schema.ConfigDescription.Messages.class
})
public class MessageDescription {

    @XmlElement(name = "Source")
    protected ItemListDescription source;
    @XmlElement(name = "Key")
    protected ItemListDescription key;
    @XmlElement(name = "Data")
    protected ItemListDescription data;
    @XmlElement(name = "Extension")
    protected MessageDescriptionExtension extension;
    @XmlAttribute(name = "IsProperty")
    protected Boolean isProperty;
    @XmlAnyAttribute
    private Map<QName, String> otherAttributes = new HashMap<QName, String>();
   // Ommited... 

As there is no @XmlRootElement, I have extends the class with a wrapper and add the annotation myself

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement (name = "MessageDescription", namespace = "http://www.onvif.org/ver10/schema")
public class MessageDescriptionXmlWrapper extends MessageDescription {
}

And I use my "wrapper" MessageDescription class to unmarshall

private static MessageDescription parseMessageDescription(final Node messageDescriptionNode) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(MessageDescriptionXmlWrapper.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    MessageDescription unmarshal = (MessageDescription) unmarshaller.unmarshal(messageDescriptionNode);
    return unmarshal;
}

The unmarshalling process is working, but one sub-sub-sub item is always set to null. I don't know where to look to fix this unmarshalling error.

The attribute who is null is the "type" who is a QName.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public static class SimpleItemDescription {

    @XmlAttribute(name = "Name", required = true)
    protected String name;
    @XmlAttribute(name = "Type", required = true)
    protected QName type;

I suspect that the QName cannot be deserialized because it's not a XML element:

-> https://docs.oracle.com/javase/7/docs/api/javax/xml/namespace/QName.html

And the original.xsd description of the SimpleItemDescription full file

        <xs:element name="SimpleItemDescription" minOccurs="0" maxOccurs="unbounded">
            <xs:annotation>
                <xs:documentation>Description of a simple item. The type must be of cathegory simpleType (xs:string, xs:integer, xs:float, ...).</xs:documentation>
            </xs:annotation>
            <xs:complexType>
                <xs:attribute name="Name" type="xs:string" use="required">
                    <xs:annotation>
                        <xs:documentation>Item name. Must be unique within a list.</xs:documentation>
                    </xs:annotation>
                </xs:attribute>
                <xs:attribute name="Type" type="xs:QName" use="required"/>
            </xs:complexType>
        </xs:element>

Any idea?

Try to add a package-info.java file

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.onvif.org/ver10/schema", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package your.package.name;

into the same folder where MessageDescriptionXmlWrapper is

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM