来自Schema的JAXB类:如何生成包含多个模式中元素的java类?

时间:2017-07-19 03:56:10

标签: java xml xsd jaxb

我有3个XML模式:

  • A.xsd (提供给我,无法修改)

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://readonly.com/cantChangeXsd" targetNamespace="http://readonly.com/cantChangeXsd" elementFormDefault="qualified" attributeFormDefault="unqualified" version="3">
    <xs:element name="RootElement" nillable="false">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="ElementA" minOccurs="0"/>
                <xs:any namespace="##other" processContents="lax" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="ElementA" nillable="false">
        <xs:annotation>
            <xs:documentation>Element exists in original schema provided to me</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:attribute name="attribute1" use="optional">
                <xs:annotation>
                    <xs:documentation>The first attribute</xs:documentation>
                </xs:annotation>
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:minLength value="1"/>
                        <xs:whiteSpace value="preserve"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="attribute2" use="optional">
                <xs:annotation>
                    <xs:documentation>The second attribute</xs:documentation>
                </xs:annotation>
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:minLength value="1"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
    

  • B.xsd (导入 A.xsd 并定义另一种特定于我的用例的复杂类型)

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sat="http://satbot.com/example" xmlns:ven="http://readonly.com/cantChangeXsd" targetNamespace="http://satbot.com/example" elementFormDefault="qualified" attributeFormDefault="unqualified" version="2.2">
    <!--************************************************************************************************-->
    <xsd:import namespace="http://readonly.com/cantChangeXsd" schemaLocation="A.xsd"/>
    <xsd:complexType name="ElementBType">
        <xsd:sequence>
            <xsd:element name="ElementBDateTime" type="xsd:dateTime"/>
        </xsd:sequence>
    </xsd:complexType>
    

  • C.xsd (包括 B.xsd 并添加定义到架构的新元素)

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://satbot.com/example" targetNamespace="http://satbot.com/example" elementFormDefault="qualified" attributeFormDefault="unqualified" version="2.2">
    <xsd:include schemaLocation="B.xsd"/>
    <xsd:element name="ElementB" type="ElementBType" nillable="false">
        <xsd:annotation>
            <xsd:documentation>Element that is added for my use-case</xsd:documentation>
        </xsd:annotation>
    </xsd:element>
    

我使用以下命令从这些模式生成java类:

xjc C.xsd -p com.satbot.xml

我正在尝试从Schema生成JAXB类,并选择 C.xsd 作为源。计划是从 C.xml 解组,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<RootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://readonly.com/cantChangeXsd" xmlns:sat="http://satbot.com/example" xsi:schemaLocation="C.xsd">
    <ElementA attribute1="value1" attribute2="value2"/>
    <sat:ElementB>
        <sat:ElementBDateTime>2017-07-21T00:00:00Z</sat:ElementBDateTime>
    </sat:ElementB>
</RootElement>

所有复杂类型都是在单独的java类中成功生成的,但我无法从< ElementB 的内容中看到任何方式来访问 ElementB 的内容strong> RootElement 类。如何访问 ElementB

生成的类包含在下面:

ElementA.java

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 
// 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: 2017.07.24 at 12:14:55 PM AEST 
//


package com.satbot.xml;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.w3c.dom.Element;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{http://readonly.com/cantChangeXsd}ElementA" minOccurs="0"/>
 *         &lt;any processContents='lax' namespace='##other' minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "elementA",
    "any"
})
@XmlRootElement(name = "RootElement")
public class RootElement {

    @XmlElement(name = "ElementA")
    protected ElementA elementA;
    @XmlAnyElement(lax = true)
    protected Object any;

    /**
     * Gets the value of the elementA property.
     * 
     * @return
     *     possible object is
     *     {@link ElementA }
     *     
     */
    public ElementA getElementA() {
        return elementA;
    }

    /**
     * Sets the value of the elementA property.
     * 
     * @param value
     *     allowed object is
     *     {@link ElementA }
     *     
     */
    public void setElementA(ElementA value) {
        this.elementA = value;
    }

    /**
     * Gets the value of the any property.
     * 
     * @return
     *     possible object is
     *     {@link Object }
     *     {@link Element }
     *     
     */
    public Object getAny() {
        return any;
    }

    /**
     * Sets the value of the any property.
     * 
     * @param value
     *     allowed object is
     *     {@link Object }
     *     {@link Element }
     *     
     */
    public void setAny(Object value) {
        this.any = value;
    }

}

ElementBType.java

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 
// 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: 2017.07.20 at 05:40:04 PM AEST 
//


package com.satbot.xml;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.datatype.XMLGregorianCalendar;


/**
 * <p>Java class for ElementBType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="ElementBType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="ElementBDateTime" type="{http://www.w3.org/2001/XMLSchema}dateTime"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ElementBType", namespace = "http://satbot.com/example", propOrder = {
    "elementBDateTime"
})
public class ElementBType {

    @XmlElement(name = "ElementBDateTime", required = true)
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar elementBDateTime;

    /**
     * Gets the value of the elementBDateTime property.
     * 
     * @return
     *     possible object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public XMLGregorianCalendar getElementBDateTime() {
        return elementBDateTime;
    }

    /**
     * Sets the value of the elementBDateTime property.
     * 
     * @param value
     *     allowed object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public void setElementBDateTime(XMLGregorianCalendar value) {
        this.elementBDateTime = value;
    }

}

ObjectFactory.java

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 
// 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: 2017.07.20 at 05:40:04 PM AEST 
//


package com.satbot.xml;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;


/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the com.satbot.xml package. 
 * <p>An ObjectFactory allows you to programatically 
 * construct new instances of the Java representation 
 * for XML content. The Java representation of XML 
 * content can consist of schema derived interfaces 
 * and classes representing the binding of schema 
 * type definitions, element declarations and model 
 * groups.  Factory methods for each of these are 
 * provided in this class.
 * 
 */
@XmlRegistry
public class ObjectFactory {

    private final static QName _ElementB_QNAME = new QName("http://satbot.com/example", "ElementB");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.satbot.xml
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link ElementBType }
     * 
     */
    public ElementBType createElementBType() {
        return new ElementBType();
    }

    /**
     * Create an instance of {@link RootElement }
     * 
     */
    public RootElement createRootElement() {
        return new RootElement();
    }

    /**
     * Create an instance of {@link ElementA }
     * 
     */
    public ElementA createElementA() {
        return new ElementA();
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link ElementBType }{@code >}}
     * 
     */
    @XmlElementDecl(namespace = "http://satbot.com/example", name = "ElementB")
    public JAXBElement<ElementBType> createElementB(ElementBType value) {
        return new JAXBElement<ElementBType>(_ElementB_QNAME, ElementBType.class, null, value);
    }

}

package-info.java

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 
// 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: 2017.07.20 at 05:40:04 PM AEST 
//

@javax.xml.bind.annotation.XmlSchema(namespace = "http://readonly.com/cantChangeXsd", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.satbot.xml;

RootElement.java

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 
// 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: 2017.07.20 at 05:40:04 PM AEST 
//


package com.satbot.xml;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{http://readonly.com/cantChangeXsd}ElementA" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "elementA"
})
@XmlRootElement(name = "RootElement")
public class RootElement {

    @XmlElement(name = "ElementA")
    protected ElementA elementA;

    /**
     * Gets the value of the elementA property.
     * 
     * @return
     *     possible object is
     *     {@link ElementA }
     *     
     */
    public ElementA getElementA() {
        return elementA;
    }

    /**
     * Sets the value of the elementA property.
     * 
     * @param value
     *     allowed object is
     *     {@link ElementA }
     *     
     */
    public void setElementA(ElementA value) {
        this.elementA = value;
    }

}

2 个答案:

答案 0 :(得分:1)

你最初离开A.xsd(<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="S1value" name="S1value" > <option value="Claim" >Claim - C</option> <option value="Title" >Title - T</option> <option value="Description" >Description - D</option> <option value="Abstract" >Abstract - A</option> <option value="Application_No" >Application_Number -APN</option> <option value="Priority_Number" >Priority_Number - PN</option> <option value="Record_Number" >Record_Number - RN</option> <option value="Priority_Date" >Priority_Date - PD</option> <option value="Application_Date" >Application_Date -APD</option> <option value="Publication_Date" >Publication_Date - PD</option> <option value="US_Class">US_Class - USC</option> <option value="IPC_Class">IPC_Class - IPC</option> <option value="CPC_Class">CPC_Class - CPC</option> <option value="Priority_Country_Code" >Priority Country Code - PCC</option> <option value="Designate_States" >Designate_States - DS</option> <option value="Legal_Status_Current" >Legal_Status_Current - LSC</option> </select>)的东西非常重要!否则,<xs:any>包含RootElement将无效。此外,当您添加此内容时,ElementB会在类xjc any的类中生成另一个名为RootElement的字段,以及Object和{ {1}}方法。通过此字段,您可以获得getAny。例如:

setAny

答案 1 :(得分:0)

我需要将XmlRootElement注释添加到 ElementB.java ,如下所示:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ElementBType", namespace = "http://satbot.com/example", propOrder = {
    "elementBDateTime"
})
@XmlRootElement(name = "ElementB")
public class ElementBType {

//ElementB definition...

}

然后将XML解组到RootElement类中,如下所示:

static RootElement unmarshallXml(File xmlFile) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(RootElement.class, ElementBType.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    RootElement rootElement = (RootElement) jaxbUnmarshaller.unmarshal(xmlFile);
    return rootElement;
}

最后,我可以使用 RootElement.java 中定义的以下getter访问ElementB

public ElementB getElementB() {
    return (ElementB) getAny();
}