XSD-10 XSD Schema 的应用场景

XSD Schema 的应用场景

  • 10 XSD Schema 的应用场景
    • 10.1 存储一些配置信息
    • 10.2 接口交互
  • 引用

10 XSD Schema 的应用场景

介绍一些应用 XSD Schema 的场景;

10.1 存储一些配置信息

如存储班级信息


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xsd:element name="class" type="SchoolClass">
    xsd:element>
    <xsd:complexType name="SchoolClass">
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="students" type="Students"/>
        xsd:sequence>
        <xsd:attribute name="id" use="required" type="xsd:string"/>
    xsd:complexType>
    <xsd:complexType name="Students">
        <xsd:sequence>
            <xsd:element maxOccurs="unbounded" name="student" type="Student"/>
        xsd:sequence>
    xsd:complexType>
    <xsd:complexType name="Student">
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
        xsd:sequence>
        <xsd:attribute name="id" use="required" type="xsd:string"/>
    xsd:complexType>
xsd:schema>

10.2 接口交互

接口请求的 xml 文档模式 callback-request.xsd


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    
    <xsd:element name="callback-request" type="CallbackRequest"/>

    <xsd:complexType name="CallbackRequest">
        <xsd:choice>
            <xsd:element name="example-v1-params"      type="ExampleV1Params"/>
            <xsd:element name="example-v2-params"      type="ExampleV1Params"/>
        xsd:choice>
        <xsd:attribute name="session-id"   type="xsd:string"  use="required"/>
        <xsd:attribute name="request-id"   type="xsd:string"  use="required"/>
        <xsd:attribute name="request-type" type="RequestEnum" use="optional"/>
        <xsd:attribute name="timeout"      type="xsd:long"    use="required"/>
    xsd:complexType>

    <xsd:complexType name="ExampleV1Params">
        <xsd:sequence>
            <xsd:element name="child-params" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
        xsd:sequence>
    xsd:complexType>

    <xsd:complexType name="ExampleV2Params">
        <xsd:sequence>
            <xsd:element name="child-params" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
        xsd:sequence>
    xsd:complexType>

    <xsd:simpleType name="RequestEnum">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="v1"/>
            <xsd:enumeration value="v2"/>
        xsd:restriction>
    xsd:simpleType>
xsd:schema>

接口返回的 xml 文档模式 callback-respond.xsd


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    
    <xsd:element name="callback-respond" type="CallbackRespond"/>

    <xsd:complexType name="CallbackRespond">
        <xsd:choice>
            <xsd:element name="example-v1-respond"        type="ExampleV1Respond"/>
            <xsd:element name="example-v2-respond"        type="ExampleV2Respond"/>
        xsd:choice>
        <xsd:attribute name="session-id" type="xsd:string" use="required"/>
        <xsd:attribute name="request-id" type="xsd:string" use="required"/>
    xsd:complexType>

    <xsd:complexType name="ExampleV1Respond">
        <xsd:sequence>
            <xsd:element name="child-params" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
        xsd:sequence>
    xsd:complexType>

    <xsd:complexType name="ExampleV2Respond">
        <xsd:sequence>
            <xsd:element name="child-params" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
        xsd:sequence>
    xsd:complexType>
xsd:schema>

一般地,将 callback-requestcallback-respond 分开放在不同 schema,通过 xjc 只会生成一个 ObjectFactory.java,然后通过编写对应的 CallbackRequestXmlSerializer.java 和 CallbackRespondXmlSerializer.java 进行解析。

样例代码: JaxbGen.rar

引用

Spring MVC with JAXB, List response based on a Generic class

Spring MVC - set JAXB marshaller property when using @ResponseBody


上一章:XSD-9 Maven + XSD
目录:学习 JAXB
下一章:无


你可能感兴趣的:(Java)