JAXB xml validation



在XSD架构中我使用schema中未定义的类的继承(出于erhitectural的原因是在其他模块/软件包中)定义为:

 <xsd:complexType name="RemoteHost">
   <xsd:annotation>
     <xsd:appinfo>  
       <jaxb:class ref="com.dummy.entities.RemoteHost"></jaxb:class>
     </xsd:appinfo>
   </xsd:annotation>
 </xsd:complexType>

例如,在同一XSD文档中定义儿童课程:

<xsd:complexType name="hostFirstSubType">
    <xsd:complexContent>
        <xsd:extension base="RemoteHost">
        </xsd:extension>        
    </xsd:complexContent>        
</xsd:complexType>
<xsd:complexType name="otherHostType">
    <xsd:complexContent>
        <xsd:extension base="RemoteHost">
            <xsd:attribute name="id" type="xsd:int" />
        </xsd:extension>            
    </xsd:complexContent>        
</xsd:complexType> 

在XSD中进行的subllass类定义为:

@SuppressWarnings("restriction")
@XmlAccessorType(XmlAccessType.NONE)
@XmlType(name = "GenericRemoteHost",propOrder = {
        "address",
        "username",
        "password",
        "sshPort"
    })
public class RemoteHost {
    @XmlElement(required = true, namespace = "http://com/dummy/entities/RemoteHost")
    protected String  address;
    @XmlElement
    protected String  username;
    @XmlElement
    protected String  password;
    @XmlElement
    protected Integer sshPort;
    ...

XML的样品部分加载:

        <hostFirstSubType>
            <rh:address>127.0.0.1</rh:address>
            <rh:username>user</rh:username>
            <rh:password>pass</rh:password>                             
            <rh:sshPort>22</rh:sshPort>             
        </hostFirstSubType>
        <otherHostType id="1">
            <rh:address>127.0.0.1</rh:address>
            <rh:username>user</rh:username>
            <rh:password>pass</rh:password>                             
            <rh:sshPort>22</rh:sshPort>             
        </otherHostType>    

我创建了一个验证收集器,以在对该模式和结果的XML文件删除XML文件期间显示所有验证错误,以某种方式令人困惑:验证收集器显示每个类的验证错误,以:

继承我的远程主机

元素" hostfirstsubtype"必须没有字符或元素 信息项[儿童],因为类型的内容类型为空。

元素" extherttype"必须没有字符或元素信息 项目[儿童],因为类型的内容类型为空

但是,当我跳过验证部分时,XML文件已正确加载到Java对象中,所有数据都加载并带有getters和setters。

作为其他一些帖子的兴趣,我尝试将@xmlaccessortype(xmlaccesstype.none)更改为其他可用值,但没有运气。

任何人都知道还有什么要寻找的...

您看到的行为与您拥有的匹配:

  1. 您的XML包含XML架构中未定义的元素,因此您会得到验证错误。
  2. 您的XML包含映射到您的对象模型的元素,这正常工作。

您可以做的事情:

  1. 包括文档中XML元素的定义到XML架构。
  2. 使您的XML元素的定义更灵活地利用any概念。

最新更新