2017-03-26 42 views
0

我是JAXB的新手,并且对JAVA来说是相当新的。我使用Eclipse内置的功能通过右键单击XSD文件从XSD文件创建了JAXB类。XSD文件使用JAXB创建嵌套静态类

但是我最终得到的是一个具有嵌套静态内部类的类。我想尝试编组并创建XML文件。但是,如何将外部类与嵌套的内部类一起传递。

public class OuterClass { 

String x = "None of the methods or variables are static in the inner our outer class"; 

public static class InnerClass{ 

} 

}

XML:应该是这样的

<OuterClass RequestType="LogOn" ApplicationSender="Example" 
WorkstationID="001"> 
<InnerClass> 
    <TimeStamp>2 009-11-20T17:30:50</TimeStamp> 
    <InterfaceVersion>01.00</InterfaceVersion> 
</InnerClass> 

还有一个结束标记,但由于某种原因在预览它没有显示出来。

下面是实际的XSD文件:

<?xml version="1.0" encoding="utf-8"?> 

<!--IFSF POS-FDC Schema 23 November 2010--> 

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.02" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

<xs:include schemaLocation="FDC_Basic_Types.xsd"/> 
<xs:include schemaLocation="FDC_DR_CurrencyCode_Full.xsd"/> 

<xs:element name="ServiceRequest"> 
    <xs:complexType mixed="true"> 
     <xs:sequence> 
      <xs:element name="POSdata"> 
       <xs:complexType> 
        <xs:sequence> 
         <xs:element name="POSTimeStamp" type="specialtime"/> 
         <xs:element name="InterfaceVersion" type="InterfaceVersion"/> 
        </xs:sequence> 
       </xs:complexType> 
      </xs:element> 
     </xs:sequence> 
      <xs:attribute name="RequestType" type="RequestType" fixed="LogOn" use="required" /> 
      <xs:attribute name="ApplicationSender" type="ApplicationSender" use="required" />   
      <xs:attribute name="WorkstationID" type="WorkstationID" use="required" />  
      <xs:attribute name="RequestID" type="RequestID" use="required" />  
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

下面是它创建的类:

<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="POSdata"> 
*   &lt;complexType> 
*    &lt;complexContent> 
*    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*     &lt;sequence> 
*     &lt;element name="POSTimeStamp" type="{}specialtime"/> 
*     &lt;element name="InterfaceVersion" type="{}InterfaceVersion"/> 
*     &lt;/sequence> 
*    &lt;/restriction> 
*    &lt;/complexContent> 
*   &lt;/complexType> 
*   &lt;/element> 
*  &lt;/sequence> 
*  &lt;attribute name="RequestType" use="required" type="{}RequestType" fixed="LogOn" /> 
*  &lt;attribute name="ApplicationSender" use="required" type="{}ApplicationSender" /> 
*  &lt;attribute name="WorkstationID" use="required" type="{}WorkstationID" /> 
*  &lt;attribute name="RequestID" use="required" type="{}RequestID" /> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "content" 
}) 
@XmlRootElement(name = "ServiceRequest") 
public class ServiceRequest { 

    @XmlElementRef(name = "POSdata", type = JAXBElement.class) 
    @XmlMixed 
    protected List<Serializable> content; 
    @XmlAttribute(name = "RequestType", required = true) 
    protected RequestType requestType; 
    @XmlAttribute(name = "ApplicationSender", required = true) 
    protected String applicationSender; 
    @XmlAttribute(name = "WorkstationID", required = true) 
    protected String workstationID; 
    @XmlAttribute(name = "RequestID", required = true) 
    protected String requestID; 

    /** 
    * Gets the value of the content 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 content property. 
    * 
    * <p> 
    * For example, to add a new item, do as follows: 
    * <pre> 
    * getContent().add(newItem); 
    * </pre> 
    * 
    * 
    * <p> 
    * Objects of the following type(s) are allowed in the list 
    * {@link JAXBElement }{@code <}{@link ServiceRequest.POSdata }{@code >} 
    * {@link String } 
    * 
    * 
    */ 
    public List<Serializable> getContent() { 
     if (content == null) { 
      content = new ArrayList<Serializable>(); 
     } 
     return this.content; 
    } 

    /** 
    * Gets the value of the requestType property. 
    * 
    * @return 
    *  possible object is 
    *  {@link RequestType } 
    *  
    */ 
    public RequestType getRequestType() { 
     if (requestType == null) { 
      return RequestType.LOG_ON; 
     } else { 
      return requestType; 
     } 
    } 

    /** 
    * Sets the value of the requestType property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link RequestType } 
    *  
    */ 
    public void setRequestType(RequestType value) { 
     this.requestType = value; 
    } 

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

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

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

    /** 
    * Sets the value of the workstationID property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setWorkstationID(String value) { 
     this.workstationID = 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="POSTimeStamp" type="{}specialtime"/> 
    *   &lt;element name="InterfaceVersion" type="{}InterfaceVersion"/> 
    *  &lt;/sequence> 
    *  &lt;/restriction> 
    * &lt;/complexContent> 
    * &lt;/complexType> 
    * </pre> 
    * 
    * 
    */ 
    @XmlAccessorType(XmlAccessType.FIELD) 
    @XmlType(name = "", propOrder = { 
     "posTimeStamp", 
     "interfaceVersion" 
    }) 
    public static class POSdata { 

     @XmlElement(name = "POSTimeStamp", required = true) 
     protected String posTimeStamp; 
     @XmlElement(name = "InterfaceVersion", required = true) 
     protected String interfaceVersion; 

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

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

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

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

    } 

} 

回答

0

完成编辑:

你都应该裹着的JAXBElement实例加入到列表content

要创建包装的JAXBElemen<POSdata>实例,您可以/应该在创建的ObjectFactory中使用工厂方法。

因此,像这样:

ServiceRequest.POSdata pos = new ServiceRequest.POSdata(); 
// fill in some data 
JAXBElement<POSdata> posElement = new ObjectFactory().createPOSdataElement(pos); // I might be a bit off with the method name, there should be one with that signature. 

request.getContent().add(posElement); 
+0

我希望我能做到这一点。但是在外部类中没有设置内部类的方法。另外我不想编辑自动生成的类。 – Saurin

+0

请粘贴实际的代码和用于生成该代码的实际xsd。 –

+0

我编辑了原文,并添加了代码。谢谢 – Saurin