Create XML Schema for Java with inheritance and Java Interfaces use -
i have collection of classes manipulating domain vehicles
here's question: need save (through parse) data xml files. have create xml schema (xsd), finding difficulties regard inheritance , interfaces.
first, seems necessary explain quickly classes:
the vehicle class abstract class , contains base attributes:
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, example, car :
public class car extends vehicle implements drivingpart { public car(); public car(string plate) { super(plate); } }
... , 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, functions well. can create objects 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 have dwelt.
edit
here attempt @ solution.
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 shipper have fleet of vehicles.
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 implementation can manipulate in java classes car, van, truck, trailer, roadtractor , semitrailer... not complex classes trailertruck , semitrailertruck. need different xsd include inheritance , interfaces. don't know how.
i found answer questions.
this xml schema. 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 see, enough change complextype vehicletype
:
- adding internal sequence attribute
minoccurs="0"
- adding attributes:
<xs:attribute name="id" type="xs:id"/>
<xs:attribute name="refid" type="xs:idref"/>
and, in xml file can this:
<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>
Comments
Post a Comment