Jaxb XmlElementRef父类标记

时间:2012-09-25 06:20:51

标签: java-ee jaxb

我需要在JAXB XmlElementRef标记生成中提供一些帮助。我有以下课程:

@XmlRootElement(name = "Request")
public class BaseRequest
{
   @XmlAttribute(name = "requestid")
   private String requestId;
   //Getters and Setters
}

@XmlRootElement(name = "ChildRequest")
public class ChildRequest extends BaseRequest
{
  private String name = null;
  private String id = null;
  //Setters and getters
}

@XmlRootElement(name = "PublishRequest")
public class PublishRequest
{
    private List<BaseRequest>   requests  = null;

    @XmlElementWrapper(name = "Requests")
    @XmlElementRefs({ @XmlElementRef(type = ChildRequest.class) })
    public List<BaseRequest> getRequests()
    {
      return this.requests;
    }

}

我希望从这些类中删除XML。

  <PublishRequest>
    <Requests>
      <Request requestid = "">
        <ChildRequest>
           <name></name>
           <id></id>
        </ChildRequest>
      </Request>
    </Requests>
  </PublishRequest>

但@XmlElementRef忽略父类属性及其声明的JAXB注释。 任何人都可以帮我生成预期的输出。

2 个答案:

答案 0 :(得分:0)

您可以使用xjc生成类:

xjc -p <your package> <your XSD>

对于您的方案,以下是XML的xsd:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="PublishRequest">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Requests">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Request">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="ChildRequest">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element type="xs:string" name="name"/>
                          <xs:element type="xs:string" name="id"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute type="xs:string" name="requestid"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

ObjectFactory类:

//
// 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: 2012.09.25 at 11:57:53 AM GMT+05:30 
//


package com.test;

import javax.xml.bind.annotation.XmlRegistry;


/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the com.test 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 {


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

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

    /**
     * Create an instance of {@link PublishRequest.Requests.Request.ChildRequest }
     * 
     */
    public PublishRequest.Requests.Request.ChildRequest createPublishRequestRequestsRequestChildRequest() {
        return new PublishRequest.Requests.Request.ChildRequest();
    }

    /**
     * Create an instance of {@link PublishRequest.Requests.Request }
     * 
     */
    public PublishRequest.Requests.Request createPublishRequestRequestsRequest() {
        return new PublishRequest.Requests.Request();
    }

    /**
     * Create an instance of {@link PublishRequest.Requests }
     * 
     */
    public PublishRequest.Requests createPublishRequestRequests() {
        return new PublishRequest.Requests();
    }

}

PublishRequest类:

//
// 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: 2012.09.25 at 11:57:53 AM GMT+05:30 
//


package com.test;

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.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 name="Requests">
 *           &lt;complexType>
 *             &lt;complexContent>
 *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                 &lt;sequence>
 *                   &lt;element name="Request">
 *                     &lt;complexType>
 *                       &lt;complexContent>
 *                         &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                           &lt;sequence>
 *                             &lt;element name="ChildRequest">
 *                               &lt;complexType>
 *                                 &lt;complexContent>
 *                                   &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *                                     &lt;sequence>
 *                                       &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                                       &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *                                     &lt;/sequence>
 *                                   &lt;/restriction>
 *                                 &lt;/complexContent>
 *                               &lt;/complexType>
 *                             &lt;/element>
 *                           &lt;/sequence>
 *                           &lt;attribute name="requestid" type="{http://www.w3.org/2001/XMLSchema}string" />
 *                         &lt;/restriction>
 *                       &lt;/complexContent>
 *                     &lt;/complexType>
 *                   &lt;/element>
 *                 &lt;/sequence>
 *               &lt;/restriction>
 *             &lt;/complexContent>
 *           &lt;/complexType>
 *         &lt;/element>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "requests"
})
@XmlRootElement(name = "PublishRequest")
public class PublishRequest {

    @XmlElement(name = "Requests", required = true)
    protected PublishRequest.Requests requests;

    /**
     * Gets the value of the requests property.
     * 
     * @return
     *     possible object is
     *     {@link PublishRequest.Requests }
     *     
     */
    public PublishRequest.Requests getRequests() {
        return requests;
    }

    /**
     * Sets the value of the requests property.
     * 
     * @param value
     *     allowed object is
     *     {@link PublishRequest.Requests }
     *     
     */
    public void setRequests(PublishRequest.Requests value) {
        this.requests = value;
    }


    /**
     * <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 name="Request">
     *           &lt;complexType>
     *             &lt;complexContent>
     *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *                 &lt;sequence>
     *                   &lt;element name="ChildRequest">
     *                     &lt;complexType>
     *                       &lt;complexContent>
     *                         &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *                           &lt;sequence>
     *                             &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *                             &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *                           &lt;/sequence>
     *                         &lt;/restriction>
     *                       &lt;/complexContent>
     *                     &lt;/complexType>
     *                   &lt;/element>
     *                 &lt;/sequence>
     *                 &lt;attribute name="requestid" type="{http://www.w3.org/2001/XMLSchema}string" />
     *               &lt;/restriction>
     *             &lt;/complexContent>
     *           &lt;/complexType>
     *         &lt;/element>
     *       &lt;/sequence>
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "request"
    })
    public static class Requests {

        @XmlElement(name = "Request", required = true)
        protected PublishRequest.Requests.Request request;

        /**
         * Gets the value of the request property.
         * 
         * @return
         *     possible object is
         *     {@link PublishRequest.Requests.Request }
         *     
         */
        public PublishRequest.Requests.Request getRequest() {
            return request;
        }

        /**
         * Sets the value of the request property.
         * 
         * @param value
         *     allowed object is
         *     {@link PublishRequest.Requests.Request }
         *     
         */
        public void setRequest(PublishRequest.Requests.Request value) {
            this.request = value;
        }


        /**
         * <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 name="ChildRequest">
         *           &lt;complexType>
         *             &lt;complexContent>
         *               &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
         *                 &lt;sequence>
         *                   &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
         *                   &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}string"/>
         *                 &lt;/sequence>
         *               &lt;/restriction>
         *             &lt;/complexContent>
         *           &lt;/complexType>
         *         &lt;/element>
         *       &lt;/sequence>
         *       &lt;attribute name="requestid" type="{http://www.w3.org/2001/XMLSchema}string" />
         *     &lt;/restriction>
         *   &lt;/complexContent>
         * &lt;/complexType>
         * </pre>
         * 
         * 
         */
        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "childRequest"
        })
        public static class Request {

            @XmlElement(name = "ChildRequest", required = true)
            protected PublishRequest.Requests.Request.ChildRequest childRequest;
            @XmlAttribute
            protected String requestid;

            /**
             * Gets the value of the childRequest property.
             * 
             * @return
             *     possible object is
             *     {@link PublishRequest.Requests.Request.ChildRequest }
             *     
             */
            public PublishRequest.Requests.Request.ChildRequest getChildRequest() {
                return childRequest;
            }

            /**
             * Sets the value of the childRequest property.
             * 
             * @param value
             *     allowed object is
             *     {@link PublishRequest.Requests.Request.ChildRequest }
             *     
             */
            public void setChildRequest(PublishRequest.Requests.Request.ChildRequest value) {
                this.childRequest = value;
            }

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

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


            /**
             * <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 name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
             *         &lt;element name="id" type="{http://www.w3.org/2001/XMLSchema}string"/>
             *       &lt;/sequence>
             *     &lt;/restriction>
             *   &lt;/complexContent>
             * &lt;/complexType>
             * </pre>
             * 
             * 
             */
            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = {
                "name",
                "id"
            })
            public static class ChildRequest {

                @XmlElement(required = true)
                protected String name;
                @XmlElement(required = true)
                protected String id;

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

                /**
                 * Sets the value of the name property.
                 * 
                 * @param value
                 *     allowed object is
                 *     {@link String }
                 *     
                 */
                public void setName(String value) {
                    this.name = 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)

在类中使用@XmlElement注释。更新了以下代码。

@XmlRootElement(name = "Request")
public class BaseRequest
{
    @XmlAttribute(name = "requestid")
    private String requestId;
    //Getters and Setters   
    @XmlElement(name = "ChildRequest")
    ChildRequest childRequest;
}

@XmlRootElement(name = "ChildRequest")
public class ChildRequest extends BaseRequest
{
    @XmlElement(name = "name")
    private String name = null;
    @XmlElement(name = "id")
    private String id = null;
    //Setters and getters 
}

@XmlRootElement(name = "PublishRequest")
public class PublishRequest
{
    private List<BaseRequest>   requests  = null;

    @XmlElementWrapper(name = "Requests")
    @XmlElementRefs({ @XmlElementRef(type = ChildRequest.class) })
    public List<BaseRequest> getRequests()
    {
        return this.requests;
    }
}