java - JAXB xml 验证

标签 java xml validation jaxb xsd

在 xsd 模式中,我使用未在模式中定义的类的继承(出于架构原因,它在其他模块/包中)定义为:

 <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 中子类化的类定义为:

@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 文件期间的所有验证错误,结果在某种程度上令人困惑: 验证收集器显示继承我的 RemoteHost 的每个类的验证错误:

Element 'hostFirstSubType' must have no character or element information item [children], because the type's content type is empty.

Element 'otherHostType' must have no character or element information item [children], because the type's content type is empty

但是,当我跳过验证部分时,xml 文件已正确加载到 java 对象中,所有数据均已加载并可通过 getter 和 setter 进行访问。

正如其他一些帖子所建议的,我尝试将 @XmlAccessorType(XmlAccessType.NONE) 更改为其他可用值,但没有成功。

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

最佳答案

您所看到的行为与您所拥有的行为相符:

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

你可以做的事情:

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

关于java - JAXB xml 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21532631/

相关文章:

c# - 如何监控(添加 - 编辑 - 删除)节点的 xml 文件

JAVA 为 xml 的所有元素添加前缀

asp.net-mvc - ASP.NET MVC + 模型状态和局部 View

c# - 嵌套成员的 ArgumentNullException

java - 正则表达式匹配所有不以 X 开头或包含 Y 的内容

java - 在JAVA中 Controller 之间发送数据?

java - 解析具有重复键的 json

java - hibernate 搜索中的 ZonedDateTime 的 DateBridge 在索引数据时出现异常

validation - 单个输入字段的自定义 JSF 验证器消息

c# - 我如何/可以使用 linq to xml 以合理的内存消耗查询巨大的 xml 文件?