java - Unmarshalled XML to abstract class, how to know the xsi:type in order to instanciate one class or another -
i have unmarshall xml(xml version 1) values , put these ones in new version of xml(xml version 2). problem there lot of tags xsi, , fact reflected in unmarshalled class abstract classes can extended various classes, , question is, way know class extends of abstract class given xml xsi:type? put here code , resources, thanks.
xml schema content data want: http://www.datex2.eu/content /xml-schema
xml source data: http://infocar.dgt.es/datex2/dgt/situationpublication/all/content.xml
xml schema of actual version: http://www.datex2.eu/content/datex-ii-xml-schema-23 finall achieve convert first xml in 1 of this.
my code(testing):
public class main { public static void main(string[] args) throws ioexception, saxexception, transformerexception, jaxbexception, xmlstreamexception, parserconfigurationexception { url url = null; try { url = new url("http://infocar.dgt.es/datex2/dgt/situationpublication/all/content.xml"); } catch (malformedurlexception e) { e.printstacktrace(); } system.out.println("abriendo url.."); urlconnection conn = url.openconnection(); system.out.println("url cargada"); system.out.println("cargando los datos necesarios..."); jaxbcontext jaxbcontext = jaxbcontext.newinstance(eu.datex.schema._1_0._1_0.d2logicalmodel.class); unmarshaller unmarshaller = jaxbcontext.createunmarshaller(); jaxbelement<eu.datex.schema._1_0._1_0.d2logicalmodel> datexmodelelement = (jaxbelement<eu.datex.schema._1_0._1_0.d2logicalmodel>) unmarshaller.unmarshal(url); eu.datex.schema._1_0._1_0.d2logicalmodel datex1 = datexmodelelement.getvalue(); eu.datex2.schema._2_0._2_3.d2logicalmodel datex2 = new eu.datex2.schema._2_0._2_3.d2logicalmodel(); situationpublication situation = (situationpublication)datex1.getpayloadpublication(); list<situation> situationlist = situation.getsituation(); (int = 0; < situationlist.size(); ++i) { list<situationrecord>situationrecordlist = situationlist.get(i).getsituationrecord(); for(int y = 0; y < situationrecordlist.size(); ++y) { //the question here, how know class extends situationrecord in other have 1 method or another? } } } }
situationrecord.class example:
// // file generated javatm architecture xml binding(jaxb) reference implementation, vjaxb 2.1.10 in jdk 6 // see <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // modifications file lost upon recompilation of source schema. // generated on: 2010.11.14 @ 03:28:36 pm pst // package eu.datex.schema._1_0._1_0; import java.math.biginteger; import java.util.arraylist; import java.util.list; import javax.xml.bind.annotation.xmlaccesstype; import javax.xml.bind.annotation.xmlaccessortype; import javax.xml.bind.annotation.xmlattribute; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlid; import javax.xml.bind.annotation.xmlschematype; import javax.xml.bind.annotation.xmlseealso; import javax.xml.bind.annotation.xmltype; import javax.xml.bind.annotation.adapters.collapsedstringadapter; import javax.xml.bind.annotation.adapters.xmljavatypeadapter; /** * identifiable instance of single record/element within situation. * * <p>java class situationrecord complex type. * * <p>the following schema fragment specifies expected content contained within class. * * <pre> * <complextype name="situationrecord"> * <complexcontent> * <restriction base="{http://www.w3.org/2001/xmlschema}anytype"> * <sequence> * <element name="situationrecordcreationreference" type="{http://datex2.eu/schema/1_0/1_0}string" minoccurs="0"/> * <element name="situationrecordcreationtime" type="{http://datex2.eu/schema/1_0/1_0}datetime"/> * <element name="situationrecordobservationtime" type="{http://datex2.eu/schema/1_0/1_0}datetime" minoccurs="0"/> * <element name="situationrecordversion" type="{http://datex2.eu/schema/1_0/1_0}nonnegativeinteger"/> * <element name="situationrecordversiontime" type="{http://datex2.eu/schema/1_0/1_0}datetime"/> * <element name="situationrecordfirstsupplierversiontime" type="{http://datex2.eu/schema/1_0/1_0}datetime"/> * <element name="informationusageoverride" type="{http://datex2.eu/schema/1_0/1_0}informationusageenum" minoccurs="0"/> * <element name="probabilityofoccurrence" type="{http://datex2.eu/schema/1_0/1_0}probabilityofoccurrenceenum"/> * <element name="sourceinformation" type="{http://datex2.eu/schema/1_0/1_0}sourceinformation" minoccurs="0"/> * <element name="validity" type="{http://datex2.eu/schema/1_0/1_0}validity"/> * <element name="impact" type="{http://datex2.eu/schema/1_0/1_0}impact" minoccurs="0"/> * <element name="cause" type="{http://datex2.eu/schema/1_0/1_0}cause" minoccurs="0"/> * <element name="advice" type="{http://datex2.eu/schema/1_0/1_0}advice" maxoccurs="unbounded" minoccurs="0"/> * <element name="generalpubliccomment" type="{http://datex2.eu/schema/1_0/1_0}comment" maxoccurs="unbounded" minoccurs="0"/> * <element name="nongeneralpubliccomment" type="{http://datex2.eu/schema/1_0/1_0}comment" maxoccurs="unbounded" minoccurs="0"/> * <element name="groupoflocations" type="{http://datex2.eu/schema/1_0/1_0}groupoflocations"/> * <element name="management" type="{http://datex2.eu/schema/1_0/1_0}management" minoccurs="0"/> * <element name="situationrecordextension" type="{http://datex2.eu/schema/1_0/1_0}extensiontype" minoccurs="0"/> * </sequence> * <attribute name="id" use="required" type="{http://www.w3.org/2001/xmlschema}id" /> * </restriction> * </complexcontent> * </complextype> * </pre> * * */ @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "situationrecord", proporder = { "situationrecordcreationreference", "situationrecordcreationtime", "situationrecordobservationtime", "situationrecordversion", "situationrecordversiontime", "situationrecordfirstsupplierversiontime", "informationusageoverride", "probabilityofoccurrence", "sourceinformation", "validity", "impact", "cause", "advice", "generalpubliccomment", "nongeneralpubliccomment", "groupoflocations", "management", "situationrecordextension" }) @xmlseealso({ nonroadeventinformation.class, operatoraction.class, trafficelement.class }) public abstract class situationrecord { protected string situationrecordcreationreference; @xmlelement(required = true) protected datetime situationrecordcreationtime; protected datetime situationrecordobservationtime; @xmlelement(required = true) protected biginteger situationrecordversion; @xmlelement(required = true) protected datetime situationrecordversiontime; @xmlelement(required = true) protected datetime situationrecordfirstsupplierversiontime; protected informationusageenum informationusageoverride; @xmlelement(required = true) protected probabilityofoccurrenceenum probabilityofoccurrence; protected sourceinformation sourceinformation; @xmlelement(required = true) protected validity validity; protected impact impact; protected cause cause; protected list<advice> advice; protected list<commentstructure> generalpubliccomment; protected list<commentstructure> nongeneralpubliccomment; @xmlelement(required = true) protected groupoflocations groupoflocations; protected management management; protected extensiontype situationrecordextension; @xmlattribute(required = true) @xmljavatypeadapter(collapsedstringadapter.class) @xmlid @xmlschematype(name = "id") protected string id; /** * gets value of situationrecordcreationreference property. * * @return * possible object * {@link string } * */ public string getsituationrecordcreationreference() { return situationrecordcreationreference; } /** * sets value of situationrecordcreationreference property. * * @param value * allowed object * {@link string } * */ public void setsituationrecordcreationreference(string value) { this.situationrecordcreationreference = value; } /** * gets value of situationrecordcreationtime property. * * @return * possible object * {@link datetime } * */ public datetime getsituationrecordcreationtime() { return situationrecordcreationtime; } /** * sets value of situationrecordcreationtime property. * * @param value * allowed object * {@link datetime } * */ public void setsituationrecordcreationtime(datetime value) { this.situationrecordcreationtime = value; } /** * gets value of situationrecordobservationtime property. * * @return * possible object * {@link datetime } * */ public datetime getsituationrecordobservationtime() { return situationrecordobservationtime; } /** * sets value of situationrecordobservationtime property. * * @param value * allowed object * {@link datetime } * */ public void setsituationrecordobservationtime(datetime value) { this.situationrecordobservationtime = value; } /** * gets value of situationrecordversion property. * * @return * possible object * {@link biginteger } * */ public biginteger getsituationrecordversion() { return situationrecordversion; } /** * sets value of situationrecordversion property. * * @param value * allowed object * {@link biginteger } * */ public void setsituationrecordversion(biginteger value) { this.situationrecordversion = value; } /** * gets value of situationrecordversiontime property. * * @return * possible object * {@link datetime } * */ public datetime getsituationrecordversiontime() { return situationrecordversiontime; } /** * sets value of situationrecordversiontime property. * * @param value * allowed object * {@link datetime } * */ public void setsituationrecordversiontime(datetime value) { this.situationrecordversiontime = value; } /** * gets value of situationrecordfirstsupplierversiontime property. * * @return * possible object * {@link datetime } * */ public datetime getsituationrecordfirstsupplierversiontime() { return situationrecordfirstsupplierversiontime; } /** * sets value of situationrecordfirstsupplierversiontime property. * * @param value * allowed object * {@link datetime } * */ public void setsituationrecordfirstsupplierversiontime(datetime value) { this.situationrecordfirstsupplierversiontime = value; } /** * gets value of informationusageoverride property. * * @return * possible object * {@link informationusageenum } * */ public informationusageenum getinformationusageoverride() { return informationusageoverride; } /** * sets value of informationusageoverride property. * * @param value * allowed object * {@link informationusageenum } * */ public void setinformationusageoverride(informationusageenum value) { this.informationusageoverride = value; } /** * gets value of probabilityofoccurrence property. * * @return * possible object * {@link probabilityofoccurrenceenum } * */ public probabilityofoccurrenceenum getprobabilityofoccurrence() { return probabilityofoccurrence; } /** * sets value of probabilityofoccurrence property. * * @param value * allowed object * {@link probabilityofoccurrenceenum } * */ public void setprobabilityofoccurrence(probabilityofoccurrenceenum value) { this.probabilityofoccurrence = value; } /** * gets value of sourceinformation property. * * @return * possible object * {@link sourceinformation } * */ public sourceinformation getsourceinformation() { return sourceinformation; } /** * sets value of sourceinformation property. * * @param value * allowed object * {@link sourceinformation } * */ public void setsourceinformation(sourceinformation value) { this.sourceinformation = value; } /** * gets value of validity property. * * @return * possible object * {@link validity } * */ public validity getvalidity() { return validity; } /** * sets value of validity property. * * @param value * allowed object * {@link validity } * */ public void setvalidity(validity value) { this.validity = value; } /** * gets value of impact property. * * @return * possible object * {@link impact } * */ public impact getimpact() { return impact; } /** * sets value of impact property. * * @param value * allowed object * {@link impact } * */ public void setimpact(impact value) { this.impact = value; } /** * gets value of cause property. * * @return * possible object * {@link cause } * */ public cause getcause() { return cause; } /** * sets value of cause property. * * @param value * allowed object * {@link cause } * */ public void setcause(cause value) { this.cause = value; } /** * gets value of advice property. * * <p> * accessor method returns reference live list, * not snapshot. therefore modification make * returned list present inside jaxb object. * why there not <code>set</code> method advice property. * * <p> * example, add new item, follows: * <pre> * getadvice().add(newitem); * </pre> * * * <p> * objects of following type(s) allowed in list * {@link advice } * * */ public list<advice> getadvice() { if (advice == null) { advice = new arraylist<advice>(); } return this.advice; } /** * gets value of generalpubliccomment property. * * <p> * accessor method returns reference live list, * not snapshot. therefore modification make * returned list present inside jaxb object. * why there not <code>set</code> method generalpubliccomment property. * * <p> * example, add new item, follows: * <pre> * getgeneralpubliccomment().add(newitem); * </pre> * * * <p> * objects of following type(s) allowed in list * {@link commentstructure.comment } * * */ public list<commentstructure> getgeneralpubliccomment() { if (generalpubliccomment == null) { generalpubliccomment = new arraylist<commentstructure>(); } return this.generalpubliccomment; } /** * gets value of nongeneralpubliccomment property. * * <p> * accessor method returns reference live list, * not snapshot. therefore modification make * returned list present inside jaxb object. * why there not <code>set</code> method nongeneralpubliccomment property. * * <p> * example, add new item, follows: * <pre> * getnongeneralpubliccomment().add(newitem); * </pre> * * * <p> * objects of following type(s) allowed in list * {@link commentstructure.comment } * * */ public list<commentstructure> getnongeneralpubliccomment() { if (nongeneralpubliccomment == null) { nongeneralpubliccomment = new arraylist<commentstructure>(); } return this.nongeneralpubliccomment; } /** * gets value of groupoflocations property. * * @return * possible object * {@link groupoflocations } * */ public groupoflocations getgroupoflocations() { return groupoflocations; } /** * sets value of groupoflocations property. * * @param value * allowed object * {@link groupoflocations } * */ public void setgroupoflocations(groupoflocations value) { this.groupoflocations = value; } /** * gets value of management property. * * @return * possible object * {@link management } * */ public management getmanagement() { return management; } /** * sets value of management property. * * @param value * allowed object * {@link management } * */ public void setmanagement(management value) { this.management = value; } /** * gets value of situationrecordextension property. * * @return * possible object * {@link extensiontype } * */ public extensiontype getsituationrecordextension() { return situationrecordextension; } /** * sets value of situationrecordextension property. * * @param value * allowed object * {@link extensiontype } * */ public void setsituationrecordextension(extensiontype value) { this.situationrecordextension = value; } /** * gets value of id property. * * @return * possible object * {@link string } * */ public string getid() { return id; } /** * sets value of id property. * * @param value * allowed object * {@link string } * */ public void setid(string value) { this.id = value; } }
jaxb able implement class given xml xsi automatically.
situationrecord.getclass().tostring()
the above line gives me name of class extends of situationrecord.class
Comments
Post a Comment