使用dom4j解析xml用例

package com.hz.kd.util;


import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.hz.fm.ws.client.WebServiceClient;
import com.hz.kd.util.CspwsStub.DoCommand;
import com.hz.kd.util.CspwsStub.DoCommandResponse;


public class XmlToMapUtil {
private static final Logger logger = LoggerFactory.getLogger(WebServiceClient.class);
/**
     * @description 将请求的应答(response)xml字符串转换成map
     * @param xmlStr
     * @return Map
     */
public static Map<String,Object> responseXmlStr2Map(String xmlStr, String paramName){
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            Document doc = DocumentHelper.parseText(xmlStr);
            Element root = doc.getRootElement();
            List children = root.elements();
            if(children != null && children.size() > 0) {
                for(int i = 0; i < children.size(); i++) {
                    if(children.get(i) instanceof Element){
                        Element child = (Element)children.get(i);
                        String nodeName = child.getName();
                        if(!child.isTextOnly()){
                            map.putAll(node2Map(child));
                        }else {
                            //没有子节点的处理方式
                            if(child.attributes().size()>0){
                                map.put(nodeName,attribute2Map(child));
                            }else{
                                map.put(child.getName(),child.getData());
                            }
                        }
                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return map;
    }


    public static Map attribute2Map(Element element){
        Map resultMap = new HashMap();
        List attributes = element.attributes();
        for(Object obj:attributes){
            Attribute attribute  = (Attribute) obj;
            resultMap.put(attribute.getName(),attribute.getText());
        }
        return resultMap;
    }




    /**
     * 将节点元素转化为Map<String,Object>
     * @param node
     * @return
     */
    public static Map<String,Object> node2Map(Element node){
        Map<String,Object> result = new HashMap();
        Map<String,Object> cMap = new HashMap();
        //将节点元素根据key的值放入map,以确定map中元素的个数,用于后续遍历使用,而key对象的value值先赋为null
        for (Object obj:node.content()){
            if(obj instanceof Element){
                Element e = (Element) obj;
                cMap.put(e.getName(),null);
            }
        }
        //遍历map
        for (Map.Entry entry:cMap.entrySet()){
            String key = entry.getKey().toString();
            List list1 = node.elements(key);
            List list2 = new ArrayList();
            for (int j = 0; j < list1.size(); j++) {
                if(list1.get(j) instanceof Element){
                    Element element = (Element) list1.get(j);
                    String name = key;
                    if (!name.equals(element.getName())){
                        name = element.getName();
                    }
                    list2.add(lastNode2Map(element));
                    cMap.put(name,list2);
                }
            }
        }
        result.put(node.getName(),cMap);
        return result;
    }


    public static Map lastNode2Map(Element element){
        Map result = new HashMap();
        List list  = new ArrayList();
        for (Object obj:element.content()){
            if(obj instanceof Element){
                Element e = (Element) obj;
                if(!e.isTextOnly()){
                    list.add(lastNode2Map(e));
                    result.put(e.getName(),list);
                }else{
                    //没有子节点的处理方式
                    if(e.attributes().size()>0){
                        result.put(e.getName(),attribute2Map(e));
                    }else{
                        result.put(e.getName(),e.getData());
                    }
                }
            }
        }
        return result;
    }
    /**
* 调用WebService
* @param req 参数对象
* @return Object
*/
public static String callServer(String xmlstr) {
CspwsStub stub = null;
try {
stub = new CspwsStub("http://10.0.1.243:9090/cspws");
} catch (org.apache.axis2.AxisFault e) {
System.out.println("1:" + e.getCause());
}
DoCommand doCommand = new DoCommand();
doCommand.setStrXMLReq(xmlstr);
DoCommandResponse resp = null;
try {
resp = stub.doCommand(doCommand);
} catch (RemoteException e) {
System.out.println("2:" + e.getCause());
}
String ret = resp.getStrXmlResp();
return ret;     
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值