【问题标题】:Unmarshalling jaxb fail on sub-elements (QName)在子元素(QName)上解组 jaxb 失败
【发布时间】:2020-05-14 13:20:10
【问题描述】:

我正在编写一个解组器,并且我正在与 jaxb 努力争取找到一个可行的解决方案。

我想解组从 .wsdl 文件生成的 MessageDescription。 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;
}

解组过程正在运行,但一个 sub-sub-sub 项始终设置为 null。我不知道在哪里可以解决这个解组错误。

为 null 的属性是 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;

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

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

以及SimpleItemDescriptionfull 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>

有什么想法吗?

【问题讨论】:

    标签: java jaxb unmarshalling


    【解决方案1】:

    尝试添加一个 package-info.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 所在的同一文件夹

    【讨论】:

    • 感谢您的回答,不幸的是这并没有改变任何东西。 Qname 对象仍然为空
    • 我在使用 jaxb-api 时遇到了类似的问题,除了定义 package-info.java 之外,还从 XmlRootElement 注释中删除了命名空间。 @XmlRootElement (name = "MessageDescription") 公共类 MessageDescriptionXmlWrapper 扩展 MessageDescription { }
    • 导入 javax.xml.bind.annotation.XmlRootElement; @XmlRootElement (name = "MessageDescription", namespace = "onvif.org/ver10/schema") public class MessageDescriptionXmlWrapper extends MessageDescription { public MessageDescriptionXmlWrapper(){ super(); } // 不要忘记构造函数 }
    • 还是不行,我找到了解决方法,将wsdl源文件中的xs:qname替换为xs:string。但我希望能找到更好的解决方案
    • 请试试这个样板:github.com/gdeignacio/onvif-utils。 XmlManagerTest.java中有测试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    相关资源
    最近更新 更多