繁体   English   中英

使用 jaxb 解组在外部 xsd 中定义的 xml 元素

[英]unmarshal xml element defined in external xsd using jaxb

我想使用 jaxb 使用此 xsd定义解组 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 在这里

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

更新:

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

OaiDcType.java 元素类型.java

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

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

如果您还为 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>)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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