有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java解组jaxb子元素失败(QName)

我正在编写一个解组器,我正在努力与jaxb进行斗争,以找到一个有效的解决方案

我想解组一个从中生成的消息描述。wsdl文件。wsdl文件中的java类是:

@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... 

由于没有@XmlRootElement,我用包装器扩展了这个类,并自己添加了注释

import javax.xml.bind.annotation.XmlRootElement;

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

我使用我的“包装器”MessageDescription类来解包

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;
}

解组过程正在工作,但有一个子项始终设置为空。我不知道该去哪里修复这个解组错误

空属性是QName的“type”

@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;

我怀疑QName不能反序列化,因为它不是XML元素:

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

还有原版。SimpleItemDescription full file的xsd描述

        <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>

知道吗


共 (1) 个答案

  1. # 1 楼答案

    尝试添加包信息。java文件

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

    进入MessageDescriptionXmlWrapper所在的文件夹