【问题标题】:unmarshal xml element defined in external xsd using jaxb使用 jaxb 在外部 xsd 中解组 xml 元素
【发布时间】:2012-11-09 13:52:49
【问题描述】:

我想使用 jaxb 解组具有此 xsd definition 的 XML 文件。

我使用 Eclipse 右键单击​​生成了 java 类,生成了 jaxb 类等。我在解组 XML 文件时没有问题。

问题是我不知道如何取消编组(映射?)元数据类型。下面是 metadataType 的 xsd 定义和生成的类:

<complexType name="metadataType">
    <annotation>
      <documentation>Metadata must be expressed in XML that complies
       with another XML Schema (namespace=#other). Metadata must be 
       explicitly qualified in the response.</documentation>
    </annotation>
    <sequence>
      <any namespace="##other" processContents="strict"/>
    </sequence>
  </complexType>

为这个类型生成的类是:

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// 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: 2012.11.08 at 05:28:26 PM PST 
//


import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlType;


/**
 * Metadata must be expressed in XML that complies
 *        with another XML Schema (namespace=#other). Metadata must be 
 *        explicitly qualified in the response.
 * 
 * <p>Java class for metadataType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="metadataType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;any namespace='##other'/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "metadataType", propOrder = {
    "any"
})
public class MetadataType {

    @XmlAnyElement(lax = true)
    protected Object any;

    /**
     * Gets the value of the any property.
     * 
     * @return
     *     possible object is
     *     {@link Object }
     *     
     */
    public Object getAny() {
        return any;
    }

    /**
     * Sets the value of the any property.
     * 
     * @param value
     *     allowed object is
     *     {@link Object }
     *     
     */
    public void setAny(Object value) {
        this.any = value;
    }

}

外部xsd为here

未编组的 XML 文档生成以下内容:

更新:

另外,我已经从外部 xsd 生成了类:

OaiDcType.java ElementType.java

这些类必须包含 MetadataType 对象的数据。

我想将 any 转换为我自己的 OaiDcType 对象,这是正确/最佳的方法吗?

【问题讨论】:

    标签: java xml xsd jaxb any


    【解决方案1】:

    JAXBContext 不知道它在 XML 中找到的元素的类型时,DOM Element 是您从 any 获得的。如果您有相关元素的 JAXB 注释类,并且 JAXBContext 知道这些以及顶级 OAI-PMH 类,则该元素将自动解组到相关类,getAny 将返回该对象而不是一个元素。

    【讨论】:

    • 必须将拳头添加到 ObjectFactory.java OaiDcType 和 ElementType 类中,因此它们将位于 JAXBContext 中。然后,JAXBContext jc = JAXBContext.newInstance("");解组器 u = jc.createUnmarshaller(); JAXBElement root = u.unmarshal(records.getDocument(), OAIPMHtype.class); OAIPMHtype 响应 = root.getValue(); MetadataType 元数据 = response.getListRecords().getRecord().get().getMetadata(); JAXBElement 元素 = (JAXBElement)metadata.getAny();
    【解决方案2】:

    如果您还为 oai_dc.xsd 模式生成了 JAXB 类,您应该能够执行另一个解组:

    MetadataType metadata = ...;
    Node oaidcNode = (Node)metadata.getAny(); // org.w3c.dom.Node
    JAXBContext oaidcContext = ...;
    Unmarshaller oaidcUnmarshaller = oaidcContext.createUnmarshaller();
    // use Unmarshaller.unmarshal(Node) or Unmarshaller.unmarshal(Node, Class<T>)
    

    【讨论】:

    • 或者更好的是,如果您将这些 oai_dc 类与 OAI-PMH 类一起包含在用于解组顶级响应的原始 JAXBContext 中,那么您将直接从getAny() 而不是获取 DOM 元素。
    • @Ian...不知道我为什么不去那里,但如果那行得通,而且只是演员阵容,那么 更好。考虑将您的评论拉出答案 - 如果您的作品有效,我很乐意放弃我的评论......
    • @IanRoberts,您的解决方案对我有用,我认为这是最好/正确的方法。来自:fusesource.com/docs/esb/4.2/jaxws/JAXWSAnyElementMapping.html。使用 @XmlAnyElement(lax = true) 您是在告诉运行时检查要解组的正确类。
    猜你喜欢
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    相关资源
    最近更新 更多