xml直接转换成java对象
Xml格式如下
Success Success 0 1_8_40_6109_20131226153050_3
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);