小编ati*_*s33的帖子

Jaxb2Marshaller模式验证似乎不起作用

我经历过Jaxb2Marshaller在进行解组时无法验证我对XSD的无效XML.我正在使用Spring 4.0.0.RELEASE和Java 7.

这是一个例子:

XSD文件:fruit.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

    <xs:attribute name="quantity" type="xs:string" />

    <xs:element name="fruit">
        <xs:complexType>
            <xs:sequence>
                <xs:choice>
                    <xs:element ref="banana" />
                    <xs:element ref="apple" />
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="banana">
        <xs:complexType>
            <xs:attribute ref="quantity" use="required" />
        </xs:complexType>
    </xs:element>

    <xs:element name="apple">
        <xs:complexType>
            <xs:attribute ref="quantity" use="required" />
        </xs:complexType>
    </xs:element>

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

我使用Spring Tool Suite从这个XSD生成了JAXB POJO来com.testraptor.xml.jaxb打包.

我的XML文件无效:invalid.xml

<?xml version="1.0" encoding="UTF-8"?>
<fruit>
    <banana quantity="5" />
    <apple quantity="3" />
</fruit>
Run Code Online (Sandbox Code Playgroud)

如你所见,我打破了架构,因为在水果标签中有一个选择,我同时使用了香蕉和苹果.

我的Spring配置文件:app-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" …
Run Code Online (Sandbox Code Playgroud)

java xml spring xsd jaxb

6
推荐指数
1
解决办法
9096
查看次数

标签 统计

java ×1

jaxb ×1

spring ×1

xml ×1

xsd ×1