- 首页 > it技术 > >
import java.io.ByteArrayInputStream;
import java.util.*;
import org.dom4j.*;
import com.newland.crm.common.util.BusinessExceptionFactory;
import com.newland.csf.common.exception.BusinessException;
import com.newland.csf.frame.util.XmlUtils;
import org.dom4j.io.SAXReader;
/**
* @author
* @Description xml解析
*/
public class XmlParseUtils {private static final String SPLIT = "/";
private Map cache ;
private String root = "";
/**
* 解析xml
*
* @param xml
*输入的xml
* @return Map 解析后的map信息
* @throws BusinessException
*/
public Map parseXml(String xml) throws BusinessException {
try {
/*if(cache != null || !cache.isEmpty()){
cache.clear();
}*/
Document doc = DocumentHelper.parseText(xml);
cache = XmlUtils.Dom2Map(doc);
return cache;
} catch (DocumentException e) {
// TODO Auto-generated catch block
throw BusinessExceptionFactory.getBusinessEx(60000054, "");
}
}
/**
* 解析xml
*
* @param xml
*输入的xml
* @return Map 解析后的map信息
* @throws BusinessException
*/
public Map parseXml2Map(String xml) throws BusinessException {
try {
Document doc = DocumentHelper.parseText(xml);
return XmlUtils.Dom2Map(doc);
} catch (DocumentException e) {
throw BusinessExceptionFactory.getBusinessEx(60000054, "");
}
}/**获取xml多层次节点
* @param map 已解析的map对象
* @param name 节点名称 多层次节点用"/"分割,exp:root/ch 即root下的ch节点
* @return list
*/
public List getNodeList(Map map, String name) {
String path = getNodePath(name);
String key = getNodeName(name);
// log("path="+path+",key="+key);
Map map1 = getNodeMap(map, path);
Object obj = map1.get(key);
return getNodeAsList(obj, key);
}/**获取xml多层次节点
* @param name 节点名称 多层次节点用"/"分割,exp:root/ch 即root下的ch节点
* @return
*/
public List getNodeList(String name){
return getNodeList(cache, name);
}
private boolean isNVL(String str) {
return str == null || str.equalsIgnoreCase("");
}/**获取对应节点下的map对象
* @param name节点名称 多层次节点用"/"分割,exp:message/ch 即message下的ch节点
* @return map
*/
public Map getNodeMap(String name){
return getNodeMap(cache,name);
}
/**根据对应map对象获取对应节点下的map对象
* @param map 已存在map对象
* @param name 节点名称 多层次节点用"/"分割,exp:root/ch 即root下的ch节点
* @return map
*/
public Map getNodeMap(Map map, String name) {
if (isNVL(name))
return map;
String[] node = splitNode(name);
String valuehttps://www.it610.com/article/= "";
Map map1 = new HashMap();
for (int i = 0;
node != null && i < node.length;
i++) {
String temp = node[i];
if (isNVL(temp))
continue;
if (i == 0)
map1 = getNodeAsMap(map.get(temp));
else
map1 = getNodeAsMap(map1.get(temp));
// log("temp="+temp+",map1="+map1.size());
if (map1 == null || map1.size() == 0)
return new HashMap();
}
return map1;
}private String getNodePath(String path) {
int d = path.lastIndexOf(SPLIT);
if (d == -1)
return "";
return path.substring(0, d);
}private String getNodeName(String path) {
int d = path.lastIndexOf(SPLIT);
if (d == -1)
return path;
return path.substring(d + 1);
}private Map getNodeAsMap(Object obj) {
if (obj == null)
return new HashMap();
if (obj.getClass().getName().equals("java.util.HashMap"))
return (Map) obj;
return new HashMap();
}private List getNodeAsList(Object obj, String key) {
List list = new ArrayList();
if (obj == null)
return list;
// log("obj="+obj.getClass().getName());
String clsname = obj.getClass().getName();
if (clsname.equals("java.util.HashMap")) {
list.add(obj);
}
else if (clsname.equals("java.util.ArrayList"))
return (List) obj;
else if (clsname.equals("java.lang.String")) {
Map map = new HashMap();
map.put(key, obj);
list.add(map);
}
return list;
}
/**
*
* 通过XML转换为Map
*
* @param xml 为String类型的Xml
* @return 第一个为Root节点,Root节点之后为Root的元素,如果为多层,可以通过key获取下一层Map
*/
public static Map createMapByXml(String xml) {
Document doc = null;
try {
doc = DocumentHelper.parseText(xml);
} catch (DocumentException e) {
e.printStackTrace();
}
Map map = new HashMap();
if (doc == null)
return map;
Element rootElement = doc.getRootElement();
elementTomap(rootElement,map);
return map;
}/***
*
* XmlToMap核心方法,里面有递归调用
*
* @param outmap
* @param outele
*/
@SuppressWarnings("unchecked")
public static Map elementTomap (Element outele,Map outmap) {
List list = outele.elements();
int size = list.size();
if(size == 0){
outmap.put(outele.getName(), outele.getTextTrim());
}else{
Map innermap = new HashMap();
for(Element ele1 : list){
String eleName = ele1.getName();
Object obj =innermap.get(eleName);
if(obj == null){
elementTomap(ele1,innermap);
}else{
if(obj instanceof java.util.Map){
List
推荐阅读