使用xjc将xml文件转换成javabean

来源:互联网 发布:简历解析软件 编辑:程序博客网 时间:2024/05/17 03:33

 1.先将xml文件转换xsd文件

原xml文件:Response.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Response>
<code>0</code>
<message>abcd</message>
</Response>

xsd文件:Response.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="Response">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="code"/>
        <xs:element ref="message"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="code" type="xs:string"/>
  <xs:element name="message" type="xs:string"/>
</xs:schema>


2.先指定一个目录存放该***.java    例:  D:\trang

cmd进入到该目录

xjc  Response.xsd   -p   Response.bean


生成的JavaBean如下:Response.java

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2017.06.07 at 08:05:42 PM CST
//


package Response.bean;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <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 ref="{}code"/>
 *         &lt;element ref="{}message"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "code",
    "message"
})
@XmlRootElement(name = "Response")
public classResponse {

    @XmlElement(required = true)
    protected String code;
    @XmlElement(required = true)
    protected String message;

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

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

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

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

}


3.将所有的xsd文件转换成javabean

xjc *.xsd -p  com.xyz..bean      (com.xyz..bean)为包名。



原创粉丝点击