从XSD文件中读取限制值

时间:2016-03-31 14:07:37

标签: java xml xsd jaxb

在以下XSD文件中,对电影评级的值有限制。是否可以从xsd中检索最小值和最大值?(最好使用JAXB绑定)

XSD:

<?xml version="1.0"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:simpleType name="RatingType">
        <xsd:annotation>
            <xsd:documentation xml:lang="en">A rating between 1 and 10 inclusive</xsd:documentation>
        </xsd:annotation>
        <xsd:restriction base="xsd:float">
            <xsd:minInclusive value="1.0" />
            <xsd:maxInclusive value="10.0" />
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:complexType name="MovieRatingType">
        <xsd:sequence>
            <xsd:element name="Title" type="xsd:string" minOccurs="1" maxOccurs="1" />
            <xsd:element name="Rating" type="RatingType" minOccurs="1" maxOccurs="1" />
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="MoviesListType">
        <xsd:sequence>
            <xsd:element name="Movie" type="MovieRatingType" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="Movies" type="MoviesListType" />

</xsd:schema>

示例XML:

<Movies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Movies.xsd">
    <Movie>
        <Title>Terminator</Title>
        <Rating>7.3</Rating>
    </Movie>
    <Movie>
        <Title>Terminator 2</Title>
        <Rating>8.9</Rating>
    </Movie>
</Movies>

这是我尝试过的:

try {

    JAXBContext jaxbContext = JAXBContext.newInstance(MoviesListType.class);
    JAXBIntrospector introspector = jaxbContext.createJAXBIntrospector();
    introspector .getElementName("Rating"). //couldn't find anything in here

    MovieRatingType. //couldn't find anything in here

    MovieRatingType instance = new MovieRatingType();
    instance. //couldn't find anything in here

    ObjectFactory factory = new ObjectFactory();
    factory. //couldn't find anything in here

} catch (JAXBException e) {
    e.printStackTrace();
}

0 个答案:

没有答案