Application scenario of XSD-10 XSD Schema

Application scenario of XSD Schema

10 XSD Schema application scenario

Introduce some scenarios of XSD Schema application;

10.1 store some configuration information

Such as storing class information

<?xml version="1.0" encoding="UTF-8"?>
<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 interface interaction

The xml document mode callback request of the interface request:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <!-- Request XML -->
    <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>

The xml document mode callback response returned by the interface:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <!-- Respond XML -->
    <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>

Generally, put the callback request and callback response in the same schema, only one ObjectFactory.java will be generated through xjc, and then parse by writing the corresponding CallbackRequestXmlSerializer.java and CallbackRespondXmlSerializer.java.

Quote

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

Spring MVC - set JAXB marshaller property when using @ResponseBody

The last chapter: XSD-9 Maven + XSD
Catalog: Learning JAXB
Next chapter: None

Published 32 original articles, won praise 4, visited 10000+
Private letter follow

Tags: Attribute xml encoding Java

Posted on Fri, 14 Feb 2020 09:11:58 -0500 by arnihr