java - 架构中的 JAXB 类 : how do I generate a java class containing elements from multiple schemas?

标签 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

我正在尝试从架构生成 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 内容的方法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);
    }

}

包信息.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;
    }

}

最佳答案

您最初在 A.xsd 中遗漏的内容( <xs:any> )非常重要!否则它对 RootElement 无效。包含ElementB 。另外,当您添加此内容时,xjc将生成另一个名为 any 的字段在类(class) RootElement ,类型为Object ,以及 getAnysetAny方法。通过这个字段你可以得到ElementB 。示例:

JAXBContext jaxbContext = JAXBContext.newInstance(RootElement.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

RootElement root;
try (InputStream in = SatBot.class.getResourceAsStream("/C.xml")) {
    root = (RootElement) unmarshaller.unmarshal(in);
}

// Get ElementB from the field 'any' in RootElement
JAXBElement<ElementBType> jaxbElement = (JAXBElement<ElementBType>) root.getAny();
ElementBType elementB = jaxbElement.getValue();

System.out.println(elementB.getElementBDateTime());

关于java - 架构中的 JAXB 类 : how do I generate a java class containing elements from multiple schemas?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45180698/

相关文章:

Java Web 启动错误 "Can not find message file"

java - apply stream 过滤掉所有满足条件的元素,除了一个

xml - 斯卡拉 XML : test for node existence and value

javascript - htmlparser2 将 xml 对象转换为字符串

java - 生成的 Java 类的 @XmlSeeAlso 注释中缺少类

xml - xjc 未按预期生成列表

web-services - 使用 JAX-WS 进行 WSDL 运行时验证

java - 扩展 Java 的链表供个人使用

java - 对按字母顺序排列的列表中的两个值重新排序。枚举/比较器

java - 如何在 spring 中找到与 spring.xml 不在同一级别的文件?