xml转换成java对象

xml直接转换成java对象
Xml格式如下
<?xml version="1.0" encoding="UTF-8"?>
<ResponseInfo>
<ResCode>Success</ResCode>
<ResMessage>Success</ResMessage>
<ErrorNo>0</ErrorNo>
<ResultToken>1_8_40_6109_20131226153050_3</ResultToken>
<Result pageindex="1">

<forms>
    <form id="0" name="0二代身份证0" ver="2013-10-18">
        <page id="0" name="身份证头像页">
            <cell id="0" name="姓名">
                <result result="张三" />
            </cell>
            <cell id="1" name="性别">
                <result result="男" />
            </cell>
            <cell id="2" name="民族">
                <result result="汉*" />
            </cell>
            <cell id="7" name="地址">
                <result result="北京市海淀区1号" />
            </cell>
            <cell id="8" name="身份证号码">
                <result result="222778778801010001" />
            </cell>
        </page>
    </form>
</forms>

</Result>
</ResponseInfo>
java类如下
package com.beisun.mbp.mbp.util.entity;

import java.io.Serializable;
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;
import lombok.Getter;
import lombok.Setter;

@XmlRootElement(name = "ResponseInfo")
@XmlType
@XmlAccessorType(XmlAccessType.NONE)
public class XmlAfrDetect implements Serializable{

    private static final long serialVersionUID = 1234567890001L;
    
    @XmlElement(name = "ResCode")
    private @Setter
    @Getter
    String ResCode;
    
    @XmlElement(name = "ResMessage")
    private @Setter
    @Getter
    String ResMessage;
    
    @XmlElement(name = "ErrorNo")
    private @Setter
    @Getter
    String ErrorNo;
    
    @XmlElement(name = "ResultToken")
    private @Setter
    @Getter
    String ResultToken;
    
    @XmlElement(name = "Result")
    private @Setter
    @Getter
    XmlAfrResult Result;    
    
}

@XmlRootElement(name = "Result")
@XmlType
@XmlAccessorType(XmlAccessType.NONE)
public class XmlAfrResult implements Serializable{

    private static final long serialVersionUID = 1234567890002L;
    
    @XmlElement(name = "Faces")
    private @Setter
    @Getter
    XmlAfrFaces Faces;
    
    @XmlElement(name = "forms")
    private @Setter
    @Getter
    XmlOcrForms forms;
}
@XmlRootElement(name = "forms")
@XmlType
@XmlAccessorType(XmlAccessType.NONE)
public class XmlOcrForms implements Serializable{
    
    
    private static final long serialVersionUID = 123456677888990L;
    
    @XmlElement(name = "form")
    private @Setter
    @Getter
    XmlOcrForm form;
}

@XmlRootElement(name = "form")
@XmlType
@XmlAccessorType(XmlAccessType.NONE)
public class XmlOcrForm implements Serializable{
    
    private static final long serialVersionUID = 12345667788899L;
    
    
    @XmlAttribute(name = "id")
    private @Setter
    @Getter
    String id;
    
    @XmlAttribute(name = "name")
    private @Setter
    @Getter
    String name;
    
    @XmlAttribute(name = "ver")
    private @Setter
    @Getter
    String ver;
    
    
    @XmlElement(name = "page")
    private @Setter
    @Getter
    XmlOcrPage page;
    
}

 

把xml直接转换成对象的代码
import javax.xml.bind.JAXB;
//protocolXML是 xml字符串哦
StringReader xml = new StringReader(protocolXML);
XmlAfrDetect responseInfo =JAXB.unmarshal(xml, XmlAfrDetect.class);

 

posted @ 2017-04-16 20:04  知行IT讲堂  阅读(3003)  评论(0编辑  收藏  举报