2012-09-25 50 views
1

我需要JAXB XmlElementRef將標籤生成一些幫助。我有類,如以下,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 ignoting父類的屬性和其宣稱的JAXB標註。 任何人都可以請求幫助我產生預期的輸出。

+0

顯示輸出XML,你現在得到的。 – basiljames

+0

它產生 沒有請求標籤 –

+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; 
       } 

      } 

     } 

    } 

} 
+0

Thank的快速回復Anshu,但在我的情況下沒有可用的請求類。而且我不能使用xjc創建類,因爲類已經定義好了。 –

+0

等待您的回覆.. –

+0

使用JAXB的最佳實踐是從已定義的_schema_生成類。你首先定義_schema_然後生成類。如果你說這些類已經定義好了,那麼我的建議就是根據你的模式重新生成正確的類。 – Anshu

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; 
    } 
}