我们正在与Jaxb合作解组一个大型深层嵌套文档。 Xjc无法使用模式,因此我们必须手动映射它。有关于测试策略的任何建议吗?
我想我想为每个嵌套类编写映射测试,理想情况是使用小型XML片段而不是完整文档的多种变体。但是,由于名称空间问题,我不知道如何执行此操作。这是我要验证的片段:
<responsibleParty>
<gmd:individualName>
<gco:CharacterString>Someones name</gco:CharacterString>
</gmd:individualName>
</responsibleParty>
我认为没有太多方法可以做到这一点。对策略有什么建议吗?
谢谢!
答案 0 :(得分:1)
如果您遇到的问题是由于嵌套类与您提供的链接具有与父类相同的名称:
然后,您可以使用外部绑定文件解决此问题:
<强> recursive.xml 强>
绑定文件允许您重命名嵌套类以避免名称冲突。
<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致电
绑定文件在XJC调用中指定:
xjc -d out -b recursive.xml recursive.xsd
主题(生成的类)
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;
}
}
}
}