zoukankan      html  css  js  c++  java
  • map转java对象

    pom依赖:

    <dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.3</version>
    </dependency>


    ConverEntity converEntity = new ConverEntity();   //映射的实体类
    BeanUtils.populate(converEntity, map); //主要方法,直接调用就可以

    此方法可以解析出soap文件并映射成java对象,网上很多帖子解析不出来标准的soap报文,这里也贴上代码


    package com.kwg.sap.utils.test;

    import com.kwg.sap.utils.MapToObjectUtil;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.commons.lang.StringUtils;
    import org.apache.commons.lang.WordUtils;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;


    public class ParseXml {

    public Map<String, Object> map = new HashMap<String, Object>();

    public Map parse(String soap) throws DocumentException {
    Document doc = DocumentHelper.parseText(soap);//报文转成doc对象
    Element root = doc.getRootElement();//获取根元素,准备递归解析这个XML树
    getCode(root);
    return map;
    }

    public void getCode(Element root) {
    if (root.elements() != null) {
    List<Element> list = root.elements();//如果当前跟节点有子节点,找到子节点
    for (Element element : list) {//遍历每个节点
    if (element.elements().size() > 0) {
    getCode(element);//当前节点不为空的话,递归遍历子节点;
    }
    if (element.elements().size() == 0) {
    //WordUtils.uncapitalize()方法是把首字符串变成小写,因为我这里需要处理map集合映射成java对象,对于映射的java命名规范,必须规范才能正确映射
    map.put(WordUtils.uncapitalize(element.getName()), element.getTextTrim());
    }//如果为叶子节点,那么直接把名字和值放入map
    }
    }
    }

    public static void main(String[] args) throws Exception {
    String soap = "<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:s="http://esb.spdbbiz.com/services/S120030432" xmlns:d="http://esb.spdbbiz.com/metadata" ><soap:Header><s:ReqHeader><d:Mac>0000000000000000</d:Mac><d:MacOrgId></d:MacOrgId><d:SourceSysId>0825</d:SourceSysId><d:ConsumerId>0825</d:ConsumerId><d:ServiceAdr>http://esb.spdbbiz.com:7701/services/S120030432</d:ServiceAdr><d:ServiceAction>urn:/UnfSocCrdtNoQry</d:ServiceAction><d:ExtendContent>00</d:ExtendContent></s:ReqHeader></soap:Header><soap:Body><s:ReqUnfSocCrdtNoQry><s:ReqSvcHeader><s:TranDate>20170913</s:TranDate><s:TranTime>162116290</s:TranTime><s:TranTellerNo>00000000</s:TranTellerNo><s:TranSeqNo></s:TranSeqNo><s:ConsumerId>0825</s:ConsumerId><s:GlobalSeqNo>15800001016431580643158064</s:GlobalSeqNo><s:SourceSysId>0825</s:SourceSysId><s:BranchId></s:BranchId><s:TerminalCode>1a</s:TerminalCode><s:CityCode>021</s:CityCode><s:AuthrTellerNo></s:AuthrTellerNo><s:AuthrPwd></s:AuthrPwd><s:AuthrCardFlag></s:AuthrCardFlag><s:AuthrCardNo></s:AuthrCardNo><s:TranCode>DM91</s:TranCode><s:PIN>1234567890123</s:PIN><s:SysOffset1>0000</s:SysOffset1><s:SysOffset2>0000</s:SysOffset2><s:MsgEndFlag>1</s:MsgEndFlag><s:MsgSeqNo>3000</s:MsgSeqNo><s:SubTranCode></s:SubTranCode><s:TranMode></s:TranMode><s:TranSerialNo>0</s:TranSerialNo></s:ReqSvcHeader><s:SvcBody><s:OrgInstId>100000024</s:OrgInstId></s:SvcBody></s:ReqUnfSocCrdtNoQry></soap:Body></soap:Envelope> ";
    //初始化报文,调用parse方法,获得结果map,然后按照需求取得字段,或者转化为其他格式
    Map map = new ParseXml().parse(soap);
    //获得字段s:SourceSysId的值;
    ConverEntity converEntity = new ConverEntity();
    MapToObjectUtil.mapToObject(map, converEntity.getClass());
    BeanUtils.populate(converEntity, map);
    System.out.println(StringUtils.isNotBlank(converEntity.getSourceSysId()));
    }


    }
  • 相关阅读:
    PAT1118:Birds in Forest
    PAT1112:Stucked Keyboard
    PAT1091:Acute Stroke
    Java基础006 --- 类、接口、对象
    Java基础005 --- 安全管理器、可变参数等
    Java基础004 --- BigInteger和BigDecimal
    Java基础003 --- 原始数据类型
    maven完整学习笔记(2)--- 依赖范围和依赖传递
    maven完整学习笔记(1)--- 基本概念及基础命令
    Java编译器007---javac 其它选项
  • 原文地址:https://www.cnblogs.com/qq1141100952com/p/11098716.html
Copyright © 2011-2022 走看看