package com.mymhotel.opera;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class XmlUtil {
/**
* 取得xml文件的根節點名稱,即消息名稱。
*
* @param xmlStr
* xml內容
* @return String 返回名稱
*/
public static String getRootName(String xmlStr) throws Exception {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new StringReader(xmlStr));
Element root = doc.getRootElement();
return root.getName();
}
/**
* 把xml文件轉換爲map形式,其中key爲有值的節點名稱,並以其所有的祖先節點爲前綴,用
* "."相連接。如:SubscribeServiceReq.Send_Address.Address_Info.DeviceType
*
* @param xmlStr
* xml內容
* @return Map 轉換爲map返回
*/
public static Map<String, String> xml2Map(String xmlStr)
throws JDOMException, IOException {
Map<String, String> rtnMap = new HashMap<String, String>();
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new StringReader(xmlStr));
// 得到根節點
Element root = doc.getRootElement();
String rootName = root.getName();
rtnMap.put("root.name", rootName);
// 調用遞歸函數,得到所有最底層元素的名稱和值,加入map中
convert(root, rtnMap, rootName);
return rtnMap;
}
/**
* 遞歸函數,找出最下層的節點並加入到map中,由xml2Map方法調用。
*
* @param e
* xml節點,包括根節點
* @param map
* 目標map
* @param lastname
* 從根節點到上一級節點名稱連接的字串
*/
public static void convert(Element e, Map<String, String> map,
String lastname) {
if (e.getAttributes().size() > 0) {
Iterator it_attr = e.getAttributes().iterator();
while (it_attr.hasNext()) {
Attribute attribute = (Attribute) it_attr.next();
String attrname = attribute.getName();
String attrvalue = e.getAttributeValue(attrname);
//map.put( attrname, attrvalue);
map.put(lastname + "." + attrname, attrvalue); //key 根據根節點 進行生成
}
}
List children = e.getChildren();
Iterator it = children.iterator();
while (it.hasNext()) {
Element child = (Element) it.next();
/*String name = lastname + "." + child.getName();*/
String name = child.getName();
// 如果有子節點,則遞歸調用
if (child.getChildren().size() > 0) {
convert(child, map, lastname + "." + child.getName());
} else {
// 如果沒有子節點,則把值加入map
map.put(name, child.getText());
// 如果該節點有屬性,則把所有的屬性值也加入map
if (child.getAttributes().size() > 0) {
Iterator attr = child.getAttributes().iterator();
while (attr.hasNext()) {
Attribute attribute = (Attribute) attr.next();
String attrname = attribute.getName();
String attrvalue = child.getAttributeValue(attrname);
map.put(lastname + "." + child.getName() + "." + attrname, attrvalue);
//map.put( attrname, attrvalue);
}
}
}
}
}
/**
* 把xml文件轉換爲list形式,其中每個元素是一個map,map中的key爲有值的節點名稱,並以其所有的祖先節點爲前綴,用
* "."相連接。如:SubscribeServiceReq.Send_Address.Address_Info.DeviceType
*
* @param xmlStr
* xml內容
* @return Map 轉換爲map返回
*/
public static List<Map<String, String>> xml2List(String xmlStr)
throws JDOMException, IOException {
List<Map<String, String>> rtnList = new ArrayList<Map<String, String>>();
Map<String, String> rtnMap = new HashMap<String, String>();
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new StringReader(xmlStr));
// 得到根節點
Element root = doc.getRootElement();
String rootName = root.getName();
rtnMap.put("root.name", rootName);
// 調用遞歸函數,得到所有最底層元素的名稱和值,加入map中
convert2List(root, rtnMap, rootName, rtnList);
if (rtnList.size() == 0)
rtnList.add(rtnMap);
return rtnList;
}
/**
* 遞歸函數,找出最下層的節點並加入到map中,如果有相同的節點,則加入list中, 由xml2List方法調用。
*
* @param e
* xml節點,包括根節點
* @param map
* 目標map
* @param lastname
* 從根節點到上一級節點名稱連接的字串
* @param list
* 相同節點生成map放入list中
*/
public static void convert2List(Element e, Map<String, String> map,
String lastname, List<Map<String, String>> list) {
if (e.getAttributes().size() > 0) {
Iterator it_attr = e.getAttributes().iterator();
while (it_attr.hasNext()) {
Attribute attribute = (Attribute) it_attr.next();
String attrname = attribute.getName();
String attrvalue = e.getAttributeValue(attrname);
map.put(attrname, attrvalue);
}
}
List children = e.getChildren();
Iterator it = children.iterator();
while (it.hasNext()) {
Element child = (Element) it.next();
String name = lastname + "." + child.getName();
// 如果有子節點,則遞歸調用
if (child.getChildren().size() > 0) {
convert(child, map, name);
} else {
// 如果沒有子節點,則把值加入map
map.put(name, child.getText());
// 如果該節點有屬性,則把所有的屬性值也加入map
if (child.getAttributes().size() > 0) {
Iterator attr = child.getAttributes().iterator();
while (attr.hasNext()) {
Attribute attribute = (Attribute) attr.next();
String attrname = attribute.getName();
String attrvalue = child.getAttributeValue(attrname);
map.put(name + "." + attrname, attrvalue);
}
}
}
// 如果有相同節點,則加入list中,不考慮子節點中又有相同節點的情況
if (e.getChildren(child.getName()).size() > 1) {
Map<String, String> aMap = new HashMap<String, String>();
aMap.putAll(map);
list.add(aMap);
map = new HashMap<String, String>();
map.put("root.name", aMap.get("root.name"));
}
}
}
/**
* 打印map 的所有key和value
*
* @param map
*/
public static void printMap(Map<String, String> map) {
Iterator<String> keys = map.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
System.out.println(key + ":" + map.get(key));
}
}
/**
* 格式化xml
*
* @param xmlStr
* @param encode
* @return String
* @date Apr 30, 2008
*//*
@SuppressWarnings("deprecation")
public static String formatXml(String xmlStr, String encode) {
SAXBuilder sb = new SAXBuilder();
Reader reader = new StringReader(xmlStr);
Document doc = null;
try {
doc = sb.build(reader);
} catch (Exception e) {
// do nosth
}
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
outputter.setEncoding(encode);
String result = "";
if (null != doc) {
result = outputter.outputString(doc);
}
return result;
}*/
}