XML字串轉Map
*
* 將xml檔案轉換成Map物件
*/
public class XmlToMap {
/**
* 獲取xml檔案轉換為的Map物件
*
* @param xmlPath xml檔案路徑
* @param keyToList 指定節點名稱轉list物件
* @return Map
*/
public static Map getPath2Map(String xmlPath, String keyToList) {
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(xmlPath));
Element root = doc.getRootElement();
Map result = new HashMap();
getElement(root, result, keyToList);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* xml字串轉換Map物件
* @param xmlStr
* @param keyToList 指定節點名稱轉list物件
* @return Map
*/
public static Map getStrXml2Map(String xmlStr, String keyToList) {
try {
Document doc = string2Doc(xmlStr);
Element root = doc.getRootElement();
Map result = new HashMap();
getElement(root, result, keyToList);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* String轉換為Document物件
*
* @paramxml字串
* @returnDocument物件
*/
private static Document string2Doc(String xmlStr) throws Exception {
java.io.Reader in = new StringReader(xmlStr);
Document doc = (new SAXBuilder()).build(in);
return doc;
}
/**
* 將Map物件轉換為xml字串
*
* @param m
* @param root 根節點名稱
* @return String
*/
public static String getMap2StrXml(Map m, String root) {
StringBuffer xml = new StringBuffer(m.size() * 3);
xml.append("<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"yes\" ?>");
xml.append("<" + root + ">");
xml.append(getElementStringXml(m));
xml.append("</" + root + ">");
return xml.toString();
}
/**
* 處理xml節點轉Map.Entry
*
* @param e
* @param m
* @param keyToList 指定節點名稱轉list物件
*/
private static void getElement(Element e, Map m, String keyToList) {
List<Element> listElement = e.getChildren();
for (Element a1 : listElement) {
String ename = a1.getName();
Object value = null;
if (a1.getChildren().size() > 0) {
Map emap = new HashMap();
getElement(a1, emap, keyToList);
value = emap;
} else {
value = a1.getValue();
}
if (m.containsKey(ename) || ename.equals(keyToList)) {
if (m.get(ename) instanceof List) {
((List) m.get(ename)).add(value);
} else {
List list = new ArrayList(10);
if(m.get(ename) != null){
list.add(m.get(ename));
}
list.add(value);
m.put(ename, list);
}
} else {
m.put(ename, value);
}
}
}
/**
* 處理map轉換字串
*
* @param m
* @return
*/
private static String getElementStringXml(Map<String, Object> m) {
StringBuffer content = new StringBuffer(m.size() * 3);
for (Iterator<Map.Entry<String, Object>> iter = m.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, Object> entry = iter.next();
Object value = entry.getValue();
if (value instanceof List) {
content.append(getElementListStringXml((List) value, entry.getKey()));
} else {
content.append("<" + entry.getKey() + ">");
if (value instanceof Map) {
content.append(getElementStringXml((Map) value));
} else {
content.append(value==null?"":value);
}
content.append("</" + entry.getKey() + ">");
}
}
return content.toString();
}
/**
* 處理list轉換xml字串
*
* @param list
* @param key
* @return
*/
private static String getElementListStringXml(List list, String key) {
StringBuffer content = new StringBuffer(list.size() * 30);
for (Object value : list) {
content.append("<" + key + ">");
if (value instanceof Map) {
content.append(getElementStringXml((Map) value));
} else {
content.append(value.toString());
}
content.append("</" + key + ">");
}
return content.toString();
}
/**
* map轉list
* @param map
* @return
*/
public static List map2List(Map map) {
List list = new ArrayList();
Iterator iter = map.entrySet().iterator(); //獲得map的Iterator
while(iter.hasNext()) {
Entry entry = (Entry)iter.next();
list.add(entry.getValue());
}
return list;
}
}