1

我正在尝试使用 Jaxb 从 xml 请求中创建一个 Java 对象,但我对 jaxb 的有限了解让我望而却步。我以前做过,但它是简单的 XML 文档,只使用基本元素,例如

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

但现在我有一个更复杂的 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>
4

2 回答 2

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插件和我的pom中的以下内容
将它编译成一个类:xjc

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

    }

}

这种帮助?

于 2013-02-20T18:01:02.623 回答
1

为您的 XML 创建 XML 模式并从中生成模型绝对是 bmorris591 所回答的,这绝对是一种方法。但是您的 XML 文档并不复杂,以至于您不能从对象开始。

CRM消息

  • 我们使用@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 类开始的一个优点是您可以利用@XmlElementWrapper注释在集合字段/属性上添加分组元素(请参阅: http ://blog.bdoughan.com/2010/09/jaxb-collection-properties.html ) . 将此与生成的DataSethttps://stackoverflow.com/a/14986770/383861进行比较。
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;

}
于 2013-02-21T10:50:00.017 回答