java - XML 请求 Jaxb

标签 java xml jaxb

我正在尝试使用 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>

最佳答案

我已经为您提供了一个快速模式,这可能不是您所需要的,因为我无法从示例数据中判断它是否允许多个或某些元素等:

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

    }

}

有帮助吗?

关于java - XML 请求 Jaxb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14986221/

相关文章:

xml - 通过命令行解析 XML

JAXB/ Jersey - 如何指定 "schemaLocation"

java - 从 Guava 的 Splitter 创建一个 String[]

java - 在不能抛出的地方抛出 NullPointerException

javascript - 这是使用 jQuery 将我的 XML 解析为 JavaScript 对象的最快方法吗?

java - JAXB - 将元素解析为字符串

java - wsimport - 没有命名空间的导入模式 ==> 名称为 'generated' 的包

java - 如何在ScrollView的底部添加字符串

java - 我想在我的小程序中添加计时器

java - 为什么&符号被解释为文本节点上的 child