zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    Ansible自动部署lnmp架构+上线电商
    elastalert 基于EFK环境的邮件报警
    冒泡法、选择法、插入法排序
    Java的基本数据类型
    cmd运行java程序找不到或无法加载主类解决办法
    JavaBean进行表单开发
    Web项目各目录详解
    【数论】整数分块及详细证明
    题解 P4874 【[USACO14DEC] Piggyback_Silver 背负式运输(银)】
    题解 P3792 【由乃与大母神原型和偶像崇拜】
  • 原文地址:https://www.cnblogs.com/nmdzwps/p/nmamtf.html
Copyright © 2011-2022 走看看