2013-02-20 60 views
1

我想使用Jaxb从一个xml请求中创建一个Java对象,但是我对jaxb的有限知识却让我失望。我以前做过这个,但它仅使用这种基本元素如XML请求Jaxb

<RootElement> 
    <Bookname>Moby Dick</Bookname> 
    <BookCode>1</BookCode> 
</RootElement> 

简单的XML文件,但现在我有一个比较复杂的XML文件,并让我开始对如何创建该对象将任何帮助不胜感激。我想我将不得不使用某种列表,以及@Xmlattribute,但我现在只是感到困惑。任何帮助将不胜感激!我希望我不只是在推翻这一点。示例XML发现如下:

<?xml version="1.0"?> 
<CRMMessage language="en_US" currency="USD" > 
<RequestSource name="Testsource" version="2" /> 
<RequestCode>GetTest</RequestCode> 
<DataSet> 
    <DataSetColumns> 
     <DSColumn name="Column1" /> 
     <DSColumn name="Column2" /> 
    </DataSetColumns> 
    <Rows> 
     <Row> 
      <Col>John</Col> 
      <Col>Doe</Col> 
     </Row> 
    </Rows> 
</DataSet> 
</CRMMessage> 
+0

您是否熟悉XML Schema? – jtahlborn 2013-02-20 17:50:18

+0

并不特别。 – parchambeau 2013-02-20 17:53:30

回答

2

我敲你了快速模式,这可能不是正是你需要的,因为我无法从示例数据超过一个或某些元素是否允许等告知:

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

    <xs:element name="CRMMessage">   
     <xs:complexType> 
      <xs:sequence> 
       <xs:element minOccurs="1" maxOccurs="1" name="RequestSource"> 
        <xs:complexType> 
         <xs:attribute name="Testsource" type="xs:string"/> 
         <xs:attribute name="version" type="xs:integer"/> 
        </xs:complexType> 
       </xs:element> 
       <xs:element minOccurs="1" maxOccurs="1" name="RequestCode" type="xs:string"/> 
       <xs:element minOccurs="1" maxOccurs="1" name="DataSet"> 
        <xs:complexType> 
         <xs:all> 
          <xs:element minOccurs="1" maxOccurs="1" name="DataSetColumns">         
           <xs:complexType>         
            <xs:sequence> 
             <xs:element minOccurs="1" maxOccurs="unbounded" name="DSColumn"> 
              <xs:complexType> 
               <xs:attribute name="name" type="xs:string"/> 
              </xs:complexType> 
             </xs:element>           
            </xs:sequence> 
           </xs:complexType> 
          </xs:element>        
          <xs:element minOccurs="1" maxOccurs="1" name="Rows">         
           <xs:complexType>         
            <xs:sequence> 
             <xs:element minOccurs="1" maxOccurs="unbounded" name="Row"> 
              <xs:complexType> 
               <xs:sequence> 
                <xs:element minOccurs="1" maxOccurs="unbounded" name="Col" type="xs:string"/>               
               </xs:sequence> 
              </xs:complexType> 
             </xs:element>           
            </xs:sequence> 
           </xs:complexType> 
          </xs:element>        
         </xs:all> 
        </xs:complexType> 
       </xs:element>          
      </xs:sequence> 
      <xs:attribute name="language" type="xs:string"/> 
      <xs:attribute name="currency" type="xs:string"/> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

您应该可以使用它作为起点。
我再编译的成通过Maven插件使用xjc和一类在我的POM如下:

<plugin> 
     <groupId>org.jvnet.jaxb2.maven2</groupId> 
     <artifactId>maven-jaxb2-plugin</artifactId> 
     <version>0.8.2</version> 
     <executions> 
      <execution> 
       <id>bind-crm</id> 
       <configuration> 
        <schemaDirectory>src/main/resources/</schemaDirectory> 
        <generatePackage>com.my.package.crm</generatePackage> 
        <forceRegenerate>true</forceRegenerate> 
       </configuration> 
       <goals> 
        <goal>generate</goal> 
       </goals> 
      </execution>                        
     </executions>     
    </plugin> 

这给了我下面的代码:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "requestSource", 
    "requestCode", 
    "dataSet" 
}) 
@XmlRootElement(name = "CRMMessage") 
public class CRMMessage { 

    @XmlElement(name = "RequestSource", required = true) 
    protected CRMMessage.RequestSource requestSource; 
    @XmlElement(name = "RequestCode", required = true) 
    protected String requestCode; 
    @XmlElement(name = "DataSet", required = true) 
    protected CRMMessage.DataSet dataSet; 
    @XmlAttribute(name = "language") 
    protected String language; 
    @XmlAttribute(name = "currency") 
    protected String currency; 

    /** 
    * Gets the value of the requestSource property. 
    * 
    * @return 
    *  possible object is 
    *  {@link CRMMessage.RequestSource } 
    *  
    */ 
    public CRMMessage.RequestSource getRequestSource() { 
     return requestSource; 
    } 

    /** 
    * Sets the value of the requestSource property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link CRMMessage.RequestSource } 
    *  
    */ 
    public void setRequestSource(CRMMessage.RequestSource value) { 
     this.requestSource = value; 
    } 

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

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

    /** 
    * Gets the value of the dataSet property. 
    * 
    * @return 
    *  possible object is 
    *  {@link CRMMessage.DataSet } 
    *  
    */ 
    public CRMMessage.DataSet getDataSet() { 
     return dataSet; 
    } 

    /** 
    * Sets the value of the dataSet property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link CRMMessage.DataSet } 
    *  
    */ 
    public void setDataSet(CRMMessage.DataSet value) { 
     this.dataSet = value; 
    } 

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

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

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

    /** 
    * Sets the value of the currency property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setCurrency(String value) { 
     this.currency = 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;all> 
    *   &lt;element name="DataSetColumns"> 
    *   &lt;complexType> 
    *    &lt;complexContent> 
    *    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *     &lt;sequence> 
    *     &lt;element name="DSColumn" maxOccurs="unbounded"> 
    *      &lt;complexType> 
    *      &lt;complexContent> 
    *       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *       &lt;attribute name="name" 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;element name="Rows"> 
    *   &lt;complexType> 
    *    &lt;complexContent> 
    *    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *     &lt;sequence> 
    *     &lt;element name="Row" maxOccurs="unbounded"> 
    *      &lt;complexType> 
    *      &lt;complexContent> 
    *       &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
    *       &lt;sequence> 
    *        &lt;element name="Col" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded"/> 
    *       &lt;/sequence> 
    *       &lt;/restriction> 
    *      &lt;/complexContent> 
    *      &lt;/complexType> 
    *     &lt;/element> 
    *     &lt;/sequence> 
    *    &lt;/restriction> 
    *    &lt;/complexContent> 
    *   &lt;/complexType> 
    *   &lt;/element> 
    *  &lt;/all> 
    *  &lt;/restriction> 
    * &lt;/complexContent> 
    * &lt;/complexType> 
    * </pre> 
    * 
    * 
    */ 
    @XmlAccessorType(XmlAccessType.FIELD) 
    @XmlType(name = "", propOrder = { 

    }) 
    public static class DataSet { 

     @XmlElement(name = "DataSetColumns", required = true) 
     protected CRMMessage.DataSet.DataSetColumns dataSetColumns; 
     @XmlElement(name = "Rows", required = true) 
     protected CRMMessage.DataSet.Rows rows; 

     /** 
     * Gets the value of the dataSetColumns property. 
     * 
     * @return 
     *  possible object is 
     *  {@link CRMMessage.DataSet.DataSetColumns } 
     *  
     */ 
     public CRMMessage.DataSet.DataSetColumns getDataSetColumns() { 
      return dataSetColumns; 
     } 

     /** 
     * Sets the value of the dataSetColumns property. 
     * 
     * @param value 
     *  allowed object is 
     *  {@link CRMMessage.DataSet.DataSetColumns } 
     *  
     */ 
     public void setDataSetColumns(CRMMessage.DataSet.DataSetColumns value) { 
      this.dataSetColumns = value; 
     } 

     /** 
     * Gets the value of the rows property. 
     * 
     * @return 
     *  possible object is 
     *  {@link CRMMessage.DataSet.Rows } 
     *  
     */ 
     public CRMMessage.DataSet.Rows getRows() { 
      return rows; 
     } 

     /** 
     * Sets the value of the rows property. 
     * 
     * @param value 
     *  allowed object is 
     *  {@link CRMMessage.DataSet.Rows } 
     *  
     */ 
     public void setRows(CRMMessage.DataSet.Rows value) { 
      this.rows = 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="DSColumn" maxOccurs="unbounded"> 
     *   &lt;complexType> 
     *    &lt;complexContent> 
     *    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
     *     &lt;attribute name="name" 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 = { 
      "dsColumn" 
     }) 
     public static class DataSetColumns { 

      @XmlElement(name = "DSColumn", required = true) 
      protected List<CRMMessage.DataSet.DataSetColumns.DSColumn> dsColumn; 

      /** 
      * Gets the value of the dsColumn 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 dsColumn property. 
      * 
      * <p> 
      * For example, to add a new item, do as follows: 
      * <pre> 
      * getDSColumn().add(newItem); 
      * </pre> 
      * 
      * 
      * <p> 
      * Objects of the following type(s) are allowed in the list 
      * {@link CRMMessage.DataSet.DataSetColumns.DSColumn } 
      * 
      * 
      */ 
      public List<CRMMessage.DataSet.DataSetColumns.DSColumn> getDSColumn() { 
       if (dsColumn == null) { 
        dsColumn = new ArrayList<CRMMessage.DataSet.DataSetColumns.DSColumn>(); 
       } 
       return this.dsColumn; 
      } 


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

       @XmlAttribute(name = "name") 
       protected String name; 

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

      } 

     } 


     /** 
     * <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="Row" maxOccurs="unbounded"> 
     *   &lt;complexType> 
     *    &lt;complexContent> 
     *    &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
     *     &lt;sequence> 
     *     &lt;element name="Col" type="{http://www.w3.org/2001/XMLSchema}string" maxOccurs="unbounded"/> 
     *     &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 = { 
      "row" 
     }) 
     public static class Rows { 

      @XmlElement(name = "Row", required = true) 
      protected List<CRMMessage.DataSet.Rows.Row> row; 

      /** 
      * Gets the value of the row 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 row property. 
      * 
      * <p> 
      * For example, to add a new item, do as follows: 
      * <pre> 
      * getRow().add(newItem); 
      * </pre> 
      * 
      * 
      * <p> 
      * Objects of the following type(s) are allowed in the list 
      * {@link CRMMessage.DataSet.Rows.Row } 
      * 
      * 
      */ 
      public List<CRMMessage.DataSet.Rows.Row> getRow() { 
       if (row == null) { 
        row = new ArrayList<CRMMessage.DataSet.Rows.Row>(); 
       } 
       return this.row; 
      } 


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

       @XmlElement(name = "Col", required = true) 
       protected List<String> col; 

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

      } 

     } 

    } 


    /** 
    * <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;attribute name="Testsource" type="{http://www.w3.org/2001/XMLSchema}string" /> 
    *  &lt;attribute name="version" type="{http://www.w3.org/2001/XMLSchema}integer" /> 
    *  &lt;/restriction> 
    * &lt;/complexContent> 
    * &lt;/complexType> 
    * </pre> 
    * 
    * 
    */ 
    @XmlAccessorType(XmlAccessType.FIELD) 
    @XmlType(name = "") 
    public static class RequestSource { 

     @XmlAttribute(name = "Testsource") 
     protected String testsource; 
     @XmlAttribute(name = "version") 
     protected BigInteger version; 

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

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

     /** 
     * Gets the value of the version property. 
     * 
     * @return 
     *  possible object is 
     *  {@link BigInteger } 
     *  
     */ 
     public BigInteger getVersion() { 
      return version; 
     } 

     /** 
     * Sets the value of the version property. 
     * 
     * @param value 
     *  allowed object is 
     *  {@link BigInteger } 
     *  
     */ 
     public void setVersion(BigInteger value) { 
      this.version = value; 
     } 

    } 

} 

帮助?

+0

非常有帮助的人,在看到你可以在这里做什么之后,我将不得不考虑制作xml模式。谢谢一堆! – parchambeau 2013-02-20 18:35:15

+0

你是如何去编译模式的?我已经加入到我的POM中,当我运行它时,我没有任何东西正在生产。再次感谢 – parchambeau 2013-02-20 18:57:32

+0

您的模式(.xsd文件)应该按照''标记中的指定位置进行定位,并且生成的包将如''标记中指定的那样。 – 2013-02-20 20:25:19

1

为您的XML创建一个XML模式并从中生成模型绝对是一种可行的方法。但是你的XML文档并不复杂,你不能从对象开始。

CRMMessage

  • 我们使用@XmlRootElement注解我们的根对象映射到文档的根元素。
  • 默认情况下,JAXB (JSR-222)实现将在公共属性(get/set方法)上查找元数据。为节省空间,我已设置@XmlAccessorType(XmlAccessType.FIELD)以指定元数据位于字段上(请参阅:http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
  • @XmlAttribute注释用于指定字段/属性映射到XML属性。
  • 默认情况下,所有字段/属性都映射到XML元素。如果默认的XML名称与您所需的映射不匹配,则可以使用@XmlElement注释来覆盖名称。
import javax.xml.bind.annotation.*; 

@XmlRootElement(name="CRMMessage") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class CRMMessage { 

    @XmlAttribute 
    private String language; 

    @XmlAttribute 
    private String currency; 

    @XmlElement(name="RequestCode") 
    private String requestCode; 

    @XmlElement(name="DataSet") 
    private DataSet dataSet; 

} 

数据集从Java类开始的

import java.util.List; 
import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD) 
public class DataSet { 

    @XmlElementWrapper(name="DataSetColumns") 
    @XmlElement(name="DSColumn") 
    private List<DSColumn> columns; 

    @XmlElementWrapper(name="Rows") 
    @XmlElement(name="Row") 
    private List<Row> rows; 

} 
+0

谢谢你的回答。这就是我想象我必须做到的。那么这个类DataSet是一个内部类,还是一个完全独立的类,您将导入到主CRMMessage中? – parchambeau 2013-02-21 14:49:46

+0

@Loeras - 'DataSet'不需要是一个静态的内部类。我会让它成为顶级的课程。 – 2013-02-21 15:14:20