简体   繁体   English

使用继承和Java接口使用创建Java的XML Schema

[英]Create XML Schema for Java with inheritance and Java Interfaces use

I have this collection of classes for manipulating this domain vehicles 我有这个类的集合来操纵这个域名车辆

Here's my question : I need to save (through parse) data to XML files. 这是我的问题 :我需要将(通过解析)数据保存到XML文件。 To do that I have to create an XML Schema (XSD), but am finding difficulties with regard to inheritance and interfaces. 为此,我必须创建一个XML Schema(XSD),但是在继承和接口方面遇到了困难。

First, it seems necessary to explain quickly my classes: 首先,似乎有必要快速解释我的课程: 在此输入图像描述

The Vehicle class is an abstract class and contains the base attributes: Vehicle类是一个抽象类,包含基本属性:

public abstract class Vehicle implements Serializable {

    public enum Stato {
        DISPONIBILE,
        NON_DISPONIBILE
    }

    private String plate;   // targa
    private String mark;    // Marca, Casa Produttrice
    private String model;   // Modello
    private String trim;    // Trim
    private float capacity; // capacità di carico
    // ... other...
    private Stato stato;    // Stato del veicolo
    private String allestimento;

    public Vehicle(){}

    public Vehicle(String plate) {
        this.plate=plate;
    }

    // Get&Set methods 
    // ...
}

And now, for example, Car : 现在,例如, 汽车

public class Car extends Vehicle implements DrivingPart {

    public Car();

    public Car(String plate) {
        super(plate);
    }

}

... and TrailerTruck : ......和TrailerTruck

public class TrailerTruck extends Vehicle {

    // TRAILER TRUCK: autocarro
    // Driving Part: Car, Van, Truck
    // Driven part: Trailer (always)

    String plateFront;
    String plateTrailer;

    DrivingPart drivingVehicle;
    Trailer trailerVehicle;

    public TrailerTruck(DrivingPart drivingVehicle, Trailer trailerVehicle) {
        plateFront=drivingVehicle.getPlate();
        plateTrailer=trailerVehicle.getPlate();
        setPlate(plateFront+" - "+plateTrailer);

        this.drivingVehicle=drivingVehicle;
        this.trailerVehicle=trailerVehicle;
    }

    @Override
    public String getAllestimento() {
        return drivingVehicle.getAllestimento()
                +", "+trailerVehicle.getAllestimento();
    }

    // ...
}

Okay, this functions well. 好的,这个功能很好。 I can easily create objects Vehicle: 我可以轻松创建对象Vehicle:

Vehicle car = new Car("AAA1");
car.setMark("Peugeot");
car.setModel("206");
car.setStato(Stato.DISPONIBILE);
//...

Vehicle truck = new Truck("AAA2");
truck.setMark("Scania");
//...

Vehicle trailer = new Trailer("TTT1");
trailer.setMark("Menci");
//...

Vehicle tt1 = new TrailerTruck((Truck) truck, (Trailer) trailer);
//...

End of illustration. 插图结束。 Sorry if I have dwelt. 对不起,如果我住的话。

EDIT 编辑

Here is my attempt at a solution . 这是我尝试解决方案

ShipperXMLSchema.xsd : ShipperXMLSchema.xsd

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

    <xs:element name="Fleet" type="FleetType"/>


    <xs:complexType name="FleetType">
        <xs:choice maxOccurs="unbounded">
            <xs:element name="Car" type="CarType"/>
            <xs:element name="Van" type="VanType"/>
            <xs:element name="Truck" type="TruckType"/>
            <xs:element name="Trailer" type="TrailerType"/>
            <xs:element name="RoadTractor" type="RoadTractorType"/> 
            <xs:element name="SemiTrailer" type="SemiTrailerType"/>
            <xs:element name="TrailerTruck" type="TrailerTruckType"/>
            <xs:element name="SemiTrailerTruck" type="SemiTrailerTruckType"/>
        </xs:choice>
        <xs:attribute name="shipperName" type="xs:string"/>
    </xs:complexType>


    <xs:complexType name="VehicleType" abstract="true">
        <xs:sequence>
            <xs:element name="plate" type="xs:string" minOccurs="1" />
            <xs:element name="mark" type="xs:string" minOccurs="0" />
            <xs:element name="model" type="xs:string" minOccurs="1" />
            <xs:element name="trim" type="xs:string" />
            <xs:element name="allestimento" type="xs:string" minOccurs="0" />
            <xs:element name="stato" type="State"/>
            <xs:element name="carryingCapacity" type="xs:float" minOccurs="0" />
            <xs:element name="ptt" type="xs:float" minOccurs="0" />
            <xs:element name="weight" type="xs:float" minOccurs="0" />
            <xs:element name="volume" type="xs:float" minOccurs="0" />
            <xs:element name="length" type="xs:float" minOccurs="0" />
            <xs:element name="height" type="xs:float" minOccurs="0" />
            <xs:element name="width" type="xs:float" minOccurs="0" />
            <xs:element name="locazioneAttuale" type="xs:string" minOccurs="0" />
        </xs:sequence>
        <xs:attribute name="id" type="xs:string"/>
    </xs:complexType>



    <!-- Definitions: tipi Car, Van, Truck, Trailer, RoadTractor, SemiTrailer -->

    <xs:complexType name="CarType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="VanType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="TruckType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="TrailerType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="RoadTractorType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>

    <xs:complexType name="SemiTrailerType">
        <xs:complexContent>
            <xs:extension base="VehicleType"/>
        </xs:complexContent>
    </xs:complexType>



    <!-- Definizione tipo TrailerTruck (autotreno) -->

    <xs:group name="DrivingPart">
        <xs:choice>
            <xs:element name="Car" type="CarType"/>
            <xs:element name="Van" type="VanType"/>
            <xs:element name="Truck" type="TruckType"/>
        </xs:choice>
    </xs:group>

    <xs:complexType name="TrailerTruckType">
        <xs:sequence>
            <xs:group ref="DrivingPart"/>
            <xs:element name="Trailer" type="TrailerType" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>



    <!-- Definition: SemiTrailerTruck -->

    <xs:complexType name="SemiTrailerTruckType" >
        <xs:sequence>
            <xs:element name="RoadTractor" type="RoadTractorType" minOccurs="1" maxOccurs="1"/>
            <xs:element name="SemiTrailer" type="SemiTrailerType" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>



    <!-- Others... -->

    <xs:simpleType name="State">
        <xs:restriction base="xs:string">
            <xs:enumeration value="DISPONIBILE"/>
            <xs:enumeration value="NON_DISPONIBILE"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

Where a Shipper have a FLEET of VEHICLES. 托运人有车辆的车队。

Shipper1.xml : Shipper1.xml

<?xml version="1.0" encoding="UTF-8"?>
<Fleet shipperName="Shipper1"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="VehicleXMLSchema.xsd"> 

    <Car id="c1">
        <plate>AAA</plate>
        <mark>Peugeot</mark>
        <model>206</model>
        <trim></trim>
        <allestimento></allestimento>
        <stato>DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>2</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Bari</locazioneAttuale>
    </Car>

    <Car>
        <plate>BBB</plate>
        <mark>Peugeot</mark>
        <model>206</model>
        <trim></trim>
        <allestimento></allestimento>
        <stato>DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>2</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Bari</locazioneAttuale>
    </Car>

    <Van>
        <plate>CCC</plate>
        <mark>Volvo</mark>
        <model></model>
        <trim></trim>
        <allestimento>frigorifero</allestimento>
        <stato>DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>3</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Barletta</locazioneAttuale>
    </Van>

    <Truck id="1">
        <plate>DDD</plate>
        <mark>Scania</mark>
        <model></model>
        <trim></trim>
        <allestimento>Frigo</allestimento>
        <stato>DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>5</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Andria</locazioneAttuale>
    </Truck>

    <Trailer id="t1">
        <plate>EEE</plate>
        <mark>Scania</mark>
        <model></model>
        <trim></trim>
        <allestimento>Frigo</allestimento>
        <stato>NON_DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>5</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Andria</locazioneAttuale>
    </Trailer>

    <RoadTractor>
        <plate>FFF</plate>
        <mark>Scania</mark>
        <model></model>
        <trim></trim>
        <allestimento>Frigo</allestimento>
        <stato>NON_DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>5</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Andria</locazioneAttuale>
    </RoadTractor>

    <SemiTrailer>
        <plate>GGG</plate>
        <mark>Scania</mark>
        <model></model>
        <trim></trim>
        <allestimento>Frigo</allestimento>
        <stato>NON_DISPONIBILE</stato>
        <carryingCapacity>0.0</carryingCapacity>
        <ptt>5</ptt>
        <weight>0.0</weight>
        <volume>0.0</volume>
        <length>0.0</length>
        <height>0.0</height>
        <width>0.0</width>
        <locazioneAttuale>Andria</locazioneAttuale>
    </SemiTrailer>


    <TrailerTruck>
        <Car id="c1">
            <plate>AAA</plate>
            <mark>Peugeot</mark>
            <model>206</model>
            <trim></trim>
            <allestimento></allestimento>
            <stato>DISPONIBILE</stato>
            <carryingCapacity>0.0</carryingCapacity>
            <ptt>2</ptt>
            <weight>0.0</weight>
            <volume>0.0</volume>
            <length>0.0</length>
            <height>0.0</height>
            <width>0.0</width>
            <locazioneAttuale>Bari</locazioneAttuale>
        </Car>
        <Trailer>
            <plate>EEE</plate>
            <mark>Scania</mark>
            <model></model>
            <trim></trim>
            <allestimento>Frigo</allestimento>
            <stato>NON_DISPONIBILE</stato>
            <carryingCapacity>0.0</carryingCapacity>
            <ptt>5</ptt>
            <weight>0.0</weight>
            <volume>0.0</volume>
            <length>0.0</length>
            <height>0.0</height>
            <width>0.0</width>
            <locazioneAttuale>Andria</locazioneAttuale>
        </Trailer>
    </TrailerTruck>


    <SemiTrailerTruck>
        <RoadTractor>
            <plate>STT1</plate>
            <mark>Scania</mark>
            <model></model>
            <trim></trim>
            <allestimento>Frigo</allestimento>
            <stato>NON_DISPONIBILE</stato>
            <carryingCapacity>0.0</carryingCapacity>
            <ptt>5</ptt>
            <weight>0.0</weight>
            <volume>0.0</volume>
            <length>0.0</length>
            <height>0.0</height>
            <width>0.0</width>
            <locazioneAttuale>Andria</locazioneAttuale>
        </RoadTractor>
        <SemiTrailer>
            <plate>ST2</plate>
            <mark>Scania</mark>
            <model></model>
            <trim></trim>
            <allestimento>Frigo</allestimento>
            <stato>NON_DISPONIBILE</stato>
            <carryingCapacity>0.0</carryingCapacity>
            <ptt>5</ptt>
            <weight>0.0</weight>
            <volume>0.0</volume>
            <length>0.0</length>
            <height>0.0</height>
            <width>0.0</width>
            <locazioneAttuale>Andria</locazioneAttuale>
        </SemiTrailer>
    </SemiTrailerTruck>

</Fleet>

With this implementation I can manipulate in Java the classes Car, Van, Truck, Trailer, RoadTractor and SemiTrailer... but not the complex classes TrailerTruck and SemiTrailerTruck. 通过这个实现,我可以在Java中操作类Car Car,Van,Truck,Trailer,RoadTractor和SemiTrailer ...但不是复杂的类TrailerTruck和SemiTrailerTruck。 I need a different XSD that include inheritance and interfaces. 我需要一个包含继承和接口的不同XSD。 But I don't know how. 但我不知道怎么做。

I found the answer to my questions. 我找到了问题的答案。

This is my XML Schema. 这是我的XML Schema。 VehicleXmlSchema.xsd : VehicleXmlSchema.xsd

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

    <xs:element name="Fleet" type="FleetType"/>

    <xs:complexType name="FleetType">
        <xs:choice maxOccurs="unbounded">
            <xs:element ref="Car"/>
            <xs:element ref="Van"/>
            <xs:element ref="Truck"/>
            <xs:element ref="Trailer"/>
            <xs:element ref="RoadTractor"/> 
            <xs:element ref="SemiTrailer"/>
            <xs:element ref="TrailerTruck"/>
            <xs:element ref="SemiTrailerTruck"/>
        </xs:choice>
        <xs:attribute name="shipperName" type="xs:string"/>
    </xs:complexType>


    <xs:complexType name="VehicleType" abstract="true">
        <xs:sequence minOccurs="0">
            <xs:element name="plate" type="xs:string" minOccurs="1" />
            <xs:element name="mark" type="xs:string" minOccurs="1" />
            <xs:element name="model" type="xs:string" minOccurs="1" />
            <xs:element name="trim" type="xs:string" minOccurs="0" />
            <xs:element name="allestimento" type="xs:string" minOccurs="0" />
            <xs:element name="stato" type="State" minOccurs="1"/>
            <xs:element name="carryingCapacity" type="xs:float" minOccurs="0" />
            <xs:element name="ptt" type="xs:float" minOccurs="0" />
            <xs:element name="weight" type="xs:float" minOccurs="0" />
            <xs:element name="volume" type="xs:float" minOccurs="0" />
            <xs:element name="length" type="xs:float" minOccurs="0" />
            <xs:element name="height" type="xs:float" minOccurs="0" />
            <xs:element name="width" type="xs:float" minOccurs="0" />
            <xs:element name="locazioneAttuale" type="xs:string" minOccurs="0" />
        </xs:sequence>
        <xs:attribute name="id" type="xs:ID"/>
        <xs:attribute name="refid" type="xs:IDREF"/>
    </xs:complexType>


    <!-- Car, Van, Truck, Trailer, RoadTractor, SemiTrailer -->

    <xs:element name="Car">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="Van">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="Truck">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="Trailer">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="RoadTractor">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

    <xs:element name="SemiTrailer">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="VehicleType"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>


    <!-- TrailerTruck -->

    <xs:group name="DrivingPart">
        <xs:choice>
            <xs:element ref="Car"/>
            <xs:element ref="Van"/>
            <xs:element ref="Truck"/>
        </xs:choice>
    </xs:group>

    <xs:element name="TrailerTruck">
        <xs:complexType>
            <xs:sequence>
                <xs:group ref="DrivingPart"/>
                <xs:element ref="Trailer" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>

    </xs:element>


    <!-- SemiTrailerTruck -->

    <xs:element name="SemiTrailerTruck">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="RoadTractor" minOccurs="1" maxOccurs="1"/>
                <xs:element ref="SemiTrailer" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>


    <!-- Altre definizioni -->

    <xs:simpleType name="State">
        <xs:restriction base="xs:string">
            <xs:enumeration value="DISPONIBILE"/>
            <xs:enumeration value="NON_DISPONIBILE"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

As you see, it was enough to change the complexType vehicleType : 如您所见,只需更改complexType vehicleType

  1. Adding to the internal sequence the attribute minOccurs="0" 添加内部序列属性minOccurs="0"
  2. Adding attributes: 添加属性:
    • <xs:attribute name="id" type="xs:ID"/>
    • <xs:attribute name="refid" type="xs:IDREF"/>

And, in a XML file I can do this: 而且,在XML文件中我可以这样做:

<Car id="car1">
    <plate>AAA</plate>
    <mark>Peugeot</mark>
    <model>206</model>
    <!-- bla bla -->
</Car>

<Truck id="truck1">
    <plate>DDD</plate>
    <mark>Scania</mark>
    <model></model>
    <!-- bla bla -->
</Truck>

<Trailer id="trailer1">
    <plate>EEE</plate>
    <mark>Scania</mark>
    <model></model>
    <!-- bla bla -->
</Trailer>

<TrailerTruck>
    <Car refid="car1"/>
    <Trailer refid="trailer1"/>
</TrailerTruck>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM