[英]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>
* <complexType name="metadataType">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <any namespace='##other'/>
* </sequence>
* </restriction>
* </complexContent>
* </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 文档生成以下内容:
更新:
另外,我从外部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.