XML请求Jaxb [英] XML Request Jaxb

查看:86
本文介绍了XML请求Jaxb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jaxb从xml请求中创建Java对象,但是我对jaxb的有限知识阻碍了我。我以前做过这个,但只使用基本元素,例如

I am trying to make a Java object out of an xml request using Jaxb but my limited knowledge of jaxb is holding me back. I have done this before but it was with simple XML documents only using basic Elements such as

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

但现在我有一个更复杂的xml文件和任何帮助让我开始如何创建这个对象将不胜感激。我想我将不得不使用某种列表,以及@Xmlattribute,但我现在只是感到困惑。任何帮助将不胜感激!我希望我不仅仅是在思考这个问题。示例XML位于以下位置:

But now I have a bit more complicated of an xml file and any help getting me started on how to create this object would be greatly appreciated. I think I will have to use some sort of list, along with @Xmlattribute, but am just confused at the moment. Any help would be greatly appreciated! I hope I am not just overthinking this. The sample XML is found below:

 <?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>


推荐答案

我已经敲了你一个快速架构,这可能不完全是你需要的,因为我无法从示例数据中判断出是否允许多个或某些元素等等:

I have knocked you up a quick schema, this may not be exactly what you need as I cannot tell from the example data whether more than one or certain elements it allowed etc:

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

您应该可以将其作为起点。

I然后使用 xjc 通过maven插件和我的pom中的以下内容将其编译成一个类:

You should be able to use that as a starting point.
I then compiled that into a class using xjc via the maven plugin and the following in my 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;
        }

    }

}



<那帮忙?

That help?

这篇关于XML请求Jaxb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆