2

jaxb を使用 して、この xsd定義で XML ファイルをアンマーシャルしたいと考えています。

Eclipse の右クリック、jaxb クラスの生成などを使用して Java クラスを生成しました。XML ファイルのアンマーシャリングに問題はありません。

ここに画像の説明を入力

問題は、MetadataType をマーシャリング (マップ?) する方法がわからないことです。以下は、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 ElementType.java

これらのクラスには、MetadataType オブジェクトのデータが含まれている必要があります。

自分の OaiDcType オブジェクトに変換したいのですが、これを行う正しい/最良の方法はどれですか?

4

2 に答える 2

1

DOMは、XML で見つかった要素の型について何も知らない場合にElement得られるものです。問題の要素に JAXB アノテーション付きクラスがあり、がこれらと最上位の OAI-PMH クラスを認識している場合、要素は自動的に関連するクラスに非整列化され、要素ではなくそのオブジェクトを返します。anyJAXBContextJAXBContextgetAny

于 2012-11-12T20:48:54.617 に答える
1

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>)
于 2012-11-12T17:35:42.860 に答える