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;
}
}
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;
}
}