将XML解组为抽象类,如何知道xsi:type以便实现一个类或另一个类

时间:2016-09-02 06:37:29

标签: java xml jaxb

我必须解组XML(XML版本1)以获取值并将这些值放入该XML的新版本(XML版本2)中。问题是xsi有很多标签,这个事实在unmarshalled类中反映为可以通过各种类扩展的抽象类,我的问题是,知道扩展的类的方法是什么? XML xsi:type给出的抽象类?我把我的代码和资源放在这里,谢谢。

内容我想要的数据的XML架构: http://www.datex2.eu/content /xml-schema

包含数据的XML源: http://infocar.dgt.es/datex2/dgt/SituationPublication/all/content.xml

实际版本的XML架构: http://www.datex2.eu/content/datex-ii-xml-schema-23我的finall实现是转换其中一个XML。

我的实际代码(测试):

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();
        for (int i = 0; i < situationList.size(); ++i)
        {
            List<SituationRecord>situationRecordList = situationList.get(i).getSituationRecord();
            for(int y = 0; y < situationRecordList.size(); ++y)
            {
               //the question is here, how to know which class extends as SituationRecord in other to have one get method or another?


            }


        }
      }
}

SituationRecord.class示例:

//
// This file was generated by the JavaTM Architecture for 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> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2010.11.14 at 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;


/**
 * An identifiable instance of a single record/element within a situation.
 * 
 * <p>Java class for SituationRecord complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="SituationRecord">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="situationRecordCreationReference" type="{http://datex2.eu/schema/1_0/1_0}String" minOccurs="0"/>
 *         &lt;element name="situationRecordCreationTime" type="{http://datex2.eu/schema/1_0/1_0}DateTime"/>
 *         &lt;element name="situationRecordObservationTime" type="{http://datex2.eu/schema/1_0/1_0}DateTime" minOccurs="0"/>
 *         &lt;element name="situationRecordVersion" type="{http://datex2.eu/schema/1_0/1_0}NonNegativeInteger"/>
 *         &lt;element name="situationRecordVersionTime" type="{http://datex2.eu/schema/1_0/1_0}DateTime"/>
 *         &lt;element name="situationRecordFirstSupplierVersionTime" type="{http://datex2.eu/schema/1_0/1_0}DateTime"/>
 *         &lt;element name="informationUsageOverride" type="{http://datex2.eu/schema/1_0/1_0}InformationUsageEnum" minOccurs="0"/>
 *         &lt;element name="probabilityOfOccurrence" type="{http://datex2.eu/schema/1_0/1_0}ProbabilityOfOccurrenceEnum"/>
 *         &lt;element name="sourceInformation" type="{http://datex2.eu/schema/1_0/1_0}SourceInformation" minOccurs="0"/>
 *         &lt;element name="validity" type="{http://datex2.eu/schema/1_0/1_0}Validity"/>
 *         &lt;element name="impact" type="{http://datex2.eu/schema/1_0/1_0}Impact" minOccurs="0"/>
 *         &lt;element name="cause" type="{http://datex2.eu/schema/1_0/1_0}Cause" minOccurs="0"/>
 *         &lt;element name="advice" type="{http://datex2.eu/schema/1_0/1_0}Advice" maxOccurs="unbounded" minOccurs="0"/>
 *         &lt;element name="generalPublicComment" type="{http://datex2.eu/schema/1_0/1_0}Comment" maxOccurs="unbounded" minOccurs="0"/>
 *         &lt;element name="nonGeneralPublicComment" type="{http://datex2.eu/schema/1_0/1_0}Comment" maxOccurs="unbounded" minOccurs="0"/>
 *         &lt;element name="groupOfLocations" type="{http://datex2.eu/schema/1_0/1_0}GroupOfLocations"/>
 *         &lt;element name="management" type="{http://datex2.eu/schema/1_0/1_0}Management" minOccurs="0"/>
 *         &lt;element name="situationRecordExtension" type="{http://datex2.eu/schema/1_0/1_0}ExtensionType" minOccurs="0"/>
 *       &lt;/sequence>
 *       &lt;attribute name="id" use="required" type="{http://www.w3.org/2001/XMLSchema}ID" />
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/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 the value of the situationRecordCreationReference property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getSituationRecordCreationReference() {
        return situationRecordCreationReference;
    }

    /**
     * Sets the value of the situationRecordCreationReference property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setSituationRecordCreationReference(String value) {
        this.situationRecordCreationReference = value;
    }

    /**
     * Gets the value of the situationRecordCreationTime property.
     * 
     * @return
     *     possible object is
     *     {@link DateTime }
     *     
     */
    public DateTime getSituationRecordCreationTime() {
        return situationRecordCreationTime;
    }

    /**
     * Sets the value of the situationRecordCreationTime property.
     * 
     * @param value
     *     allowed object is
     *     {@link DateTime }
     *     
     */
    public void setSituationRecordCreationTime(DateTime value) {
        this.situationRecordCreationTime = value;
    }

    /**
     * Gets the value of the situationRecordObservationTime property.
     * 
     * @return
     *     possible object is
     *     {@link DateTime }
     *     
     */
    public DateTime getSituationRecordObservationTime() {
        return situationRecordObservationTime;
    }

    /**
     * Sets the value of the situationRecordObservationTime property.
     * 
     * @param value
     *     allowed object is
     *     {@link DateTime }
     *     
     */
    public void setSituationRecordObservationTime(DateTime value) {
        this.situationRecordObservationTime = value;
    }

    /**
     * Gets the value of the situationRecordVersion property.
     * 
     * @return
     *     possible object is
     *     {@link BigInteger }
     *     
     */
    public BigInteger getSituationRecordVersion() {
        return situationRecordVersion;
    }

    /**
     * Sets the value of the situationRecordVersion property.
     * 
     * @param value
     *     allowed object is
     *     {@link BigInteger }
     *     
     */
    public void setSituationRecordVersion(BigInteger value) {
        this.situationRecordVersion = value;
    }

    /**
     * Gets the value of the situationRecordVersionTime property.
     * 
     * @return
     *     possible object is
     *     {@link DateTime }
     *     
     */
    public DateTime getSituationRecordVersionTime() {
        return situationRecordVersionTime;
    }

    /**
     * Sets the value of the situationRecordVersionTime property.
     * 
     * @param value
     *     allowed object is
     *     {@link DateTime }
     *     
     */
    public void setSituationRecordVersionTime(DateTime value) {
        this.situationRecordVersionTime = value;
    }

    /**
     * Gets the value of the situationRecordFirstSupplierVersionTime property.
     * 
     * @return
     *     possible object is
     *     {@link DateTime }
     *     
     */
    public DateTime getSituationRecordFirstSupplierVersionTime() {
        return situationRecordFirstSupplierVersionTime;
    }

    /**
     * Sets the value of the situationRecordFirstSupplierVersionTime property.
     * 
     * @param value
     *     allowed object is
     *     {@link DateTime }
     *     
     */
    public void setSituationRecordFirstSupplierVersionTime(DateTime value) {
        this.situationRecordFirstSupplierVersionTime = value;
    }

    /**
     * Gets the value of the informationUsageOverride property.
     * 
     * @return
     *     possible object is
     *     {@link InformationUsageEnum }
     *     
     */
    public InformationUsageEnum getInformationUsageOverride() {
        return informationUsageOverride;
    }

    /**
     * Sets the value of the informationUsageOverride property.
     * 
     * @param value
     *     allowed object is
     *     {@link InformationUsageEnum }
     *     
     */
    public void setInformationUsageOverride(InformationUsageEnum value) {
        this.informationUsageOverride = value;
    }

    /**
     * Gets the value of the probabilityOfOccurrence property.
     * 
     * @return
     *     possible object is
     *     {@link ProbabilityOfOccurrenceEnum }
     *     
     */
    public ProbabilityOfOccurrenceEnum getProbabilityOfOccurrence() {
        return probabilityOfOccurrence;
    }

    /**
     * Sets the value of the probabilityOfOccurrence property.
     * 
     * @param value
     *     allowed object is
     *     {@link ProbabilityOfOccurrenceEnum }
     *     
     */
    public void setProbabilityOfOccurrence(ProbabilityOfOccurrenceEnum value) {
        this.probabilityOfOccurrence = value;
    }

    /**
     * Gets the value of the sourceInformation property.
     * 
     * @return
     *     possible object is
     *     {@link SourceInformation }
     *     
     */
    public SourceInformation getSourceInformation() {
        return sourceInformation;
    }

    /**
     * Sets the value of the sourceInformation property.
     * 
     * @param value
     *     allowed object is
     *     {@link SourceInformation }
     *     
     */
    public void setSourceInformation(SourceInformation value) {
        this.sourceInformation = value;
    }

    /**
     * Gets the value of the validity property.
     * 
     * @return
     *     possible object is
     *     {@link Validity }
     *     
     */
    public Validity getValidity() {
        return validity;
    }

    /**
     * Sets the value of the validity property.
     * 
     * @param value
     *     allowed object is
     *     {@link Validity }
     *     
     */
    public void setValidity(Validity value) {
        this.validity = value;
    }

    /**
     * Gets the value of the impact property.
     * 
     * @return
     *     possible object is
     *     {@link Impact }
     *     
     */
    public Impact getImpact() {
        return impact;
    }

    /**
     * Sets the value of the impact property.
     * 
     * @param value
     *     allowed object is
     *     {@link Impact }
     *     
     */
    public void setImpact(Impact value) {
        this.impact = value;
    }

    /**
     * Gets the value of the cause property.
     * 
     * @return
     *     possible object is
     *     {@link Cause }
     *     
     */
    public Cause getCause() {
        return cause;
    }

    /**
     * Sets the value of the cause property.
     * 
     * @param value
     *     allowed object is
     *     {@link Cause }
     *     
     */
    public void setCause(Cause value) {
        this.cause = value;
    }

    /**
     * Gets the value of the advice property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the advice property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getAdvice().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Advice }
     * 
     * 
     */
    public List<Advice> getAdvice() {
        if (advice == null) {
            advice = new ArrayList<Advice>();
        }
        return this.advice;
    }

    /**
     * Gets the value of the generalPublicComment property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the generalPublicComment property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getGeneralPublicComment().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link CommentStructure.Comment }
     * 
     * 
     */
    public List<CommentStructure> getGeneralPublicComment() {
        if (generalPublicComment == null) {
            generalPublicComment = new ArrayList<CommentStructure>();
        }
        return this.generalPublicComment;
    }

    /**
     * Gets the value of the nonGeneralPublicComment property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the nonGeneralPublicComment property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getNonGeneralPublicComment().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link CommentStructure.Comment }
     * 
     * 
     */
    public List<CommentStructure> getNonGeneralPublicComment() {
        if (nonGeneralPublicComment == null) {
            nonGeneralPublicComment = new ArrayList<CommentStructure>();
        }
        return this.nonGeneralPublicComment;
    }

    /**
     * Gets the value of the groupOfLocations property.
     * 
     * @return
     *     possible object is
     *     {@link GroupOfLocations }
     *     
     */
    public GroupOfLocations getGroupOfLocations() {
        return groupOfLocations;
    }

    /**
     * Sets the value of the groupOfLocations property.
     * 
     * @param value
     *     allowed object is
     *     {@link GroupOfLocations }
     *     
     */
    public void setGroupOfLocations(GroupOfLocations value) {
        this.groupOfLocations = value;
    }

    /**
     * Gets the value of the management property.
     * 
     * @return
     *     possible object is
     *     {@link Management }
     *     
     */
    public Management getManagement() {
        return management;
    }

    /**
     * Sets the value of the management property.
     * 
     * @param value
     *     allowed object is
     *     {@link Management }
     *     
     */
    public void setManagement(Management value) {
        this.management = value;
    }

    /**
     * Gets the value of the situationRecordExtension property.
     * 
     * @return
     *     possible object is
     *     {@link ExtensionType }
     *     
     */
    public ExtensionType getSituationRecordExtension() {
        return situationRecordExtension;
    }

    /**
     * Sets the value of the situationRecordExtension property.
     * 
     * @param value
     *     allowed object is
     *     {@link ExtensionType }
     *     
     */
    public void setSituationRecordExtension(ExtensionType value) {
        this.situationRecordExtension = value;
    }

    /**
     * Gets the value of the id property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getId() {
        return id;
    }

    /**
     * Sets the value of the id property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setId(String value) {
        this.id = value;
    }

}

1 个答案:

答案 0 :(得分:0)

JAXB能够自动实现XML xsi给出的类。

<强> situationRecord.getClass()。的toString()

上面的行给出了scenarioRecord.class

扩展的类的名称