Jaxb strategy for testing large document mappings
We are working with Jaxb to unmarshall a large deeply nested document. Xjc wouldn't work with the schema so we are having to map it by hand. Any suggestions for testing strategies here?
I think I would like to to write mapping tests for each nested class as I go, ideally with using small XML fragments rather that many variants of fu开发者_StackOverflowll documents. However I don't see how to do this due to namespace problems. This is a fragment I would like to validate:
<responsibleParty>
<gmd:individualName>
<gco:CharacterString>Someones name</gco:CharacterString>
</gmd:individualName>
</responsibleParty>
I don't see much of a way to do this. Any suggestions for strategies here?
Thanks!
If the problem you are seeing is due to nested classes having the same name as parent classes as per the link you provided:
- https://forums.oracle.com/forums/thread.jspa?messageID=9259888
Then you can workaround this issue by using an external bindings file:
recursive.xml
The binding file allows you to rename the nested classes to avoid the name conflict.
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:bindings schemaLocation="recursive.xsd">
<jaxb:bindings node="/xsd:schema/xsd:element[@name='topic']/xsd:complexType/xsd:sequence/xsd:element[@name='topic']/xsd:complexType">
<jaxb:class name="Topic2"/>
</jaxb:bindings>
<jaxb:bindings node="/xsd:schema/xsd:element[@name='topic']/xsd:complexType/xsd:sequence/xsd:element[@name='topic']/xsd:complexType/xsd:sequence/xsd:element[@name='topic']/xsd:complexType">
<jaxb:class name="Topic3"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
recursive.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="topic">
<xs:complexType>
<xs:sequence>
<xs:element name="topic" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="topic" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="topic" minOccurs="0" maxOccurs="unbounded" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XJC call
The binding file is specified in the XJC call:
xjc -d out -b recursive.xml recursive.xsd
Topic (Generated Class)
package generated;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"topic"
})
@XmlRootElement(name = "topic")
public class Topic {
@XmlElement(required = true)
protected Topic.Topic2 topic;
public Topic.Topic2 getTopic() {
return topic;
}
public void setTopic(Topic.Topic2 value) {
this.topic = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"topic"
})
public static class Topic2 {
@XmlElement(required = true)
protected Topic.Topic2 .Topic3 topic;
public Topic.Topic2 .Topic3 getTopic() {
return topic;
}
public void setTopic(Topic.Topic2 .Topic3 value) {
this.topic = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"topic"
})
public static class Topic3 {
protected List<String> topic;
public List<String> getTopic() {
if (topic == null) {
topic = new ArrayList<String>();
}
return this.topic;
}
}
}
}
精彩评论