The DOM Element
is what you get from an any
when the JAXBContext
doesn't know anything about the type of the element it found in the XML. If you have JAXB annotated classes for the element in question and the JAXBContext
knows about these as well as the top level OAI-PMH classes then the element will automatically get unmarshalled to the relevant class and getAny
will return that object rather than an Element.
unmarshal xml element defined in external xsd using jaxb
Pergunta
I would like to unmarshal an XML file with this xsd definition using jaxb.
I have generated java classes using eclipse right click, generate jaxb classes, etc. I have no problems unmarshalling XML files.
The problem is that I don't know how to un marshal (map?) MetadataType. Below is the xsd definition for metadataType and the class generated:
<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>
Generated class for this Type is:
//
// 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;
}
}
The external xsd for is here
The unmarshalled XML doc generate this:
UPDATE:
Also, I've generated classes from the external xsd:
OaiDcType.java ElementType.java
Those classes must contain the data of MetadataType object.
I would like to convert any to my own OaiDcType object, wich is the correct/best way to do this?
Solução
Outras dicas
If you also have JAXB classes generated for the oai_dc.xsd schema, you should be able to just perform another unmarshal:
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>)