import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
public class ParseXml {
/**
* xml转换成TreeMap
* @param xml
* @return
*/
public static Map xmlToTreeMap(String xml) {
Map map = new TreeMap();
try {
if (xml == null || "".equals(xml.replaceAll(" ", ""))) {
return null;
} else {
Document doc = (new SAXBuilder()).build(new StringReader(xml));
Element element = doc.getRootElement();
if (element == null) {
throw new Exception("XML包格式错误:没有根元素");
}
List it = element.getChildren();
int len = it.size();
if (len == 0) {
throw new Exception("XML包格式错误:没有子元素");
}
map = new TreeMap();
for (int i = 0; i < len; i++) {
Element subelement = (Element) it.get(i);
List itc = subelement.getChildren();
if (itc != null && itc.size() > 0) {
for (int n = 0; n < itc.size(); n++) {
Element sub = (Element) itc.get(n);
map.put(sub.getName().toLowerCase(), sub.getText());
}
}
map.put(subelement.getName().toLowerCase(), subelement.getText());
}
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* xml转换成HashMap
*
* @param xml
* @return
*/
public static Map xmlToHashMap(String xml) {
Map map = new HashMap();
try {
if (xml == null || "".equals(xml.replaceAll(" ", ""))) {
return null;
} else {
Document doc = (new SAXBuilder()).build(new StringReader(xml));
Element element = doc.getRootElement();
if (element == null) {
throw new Exception("XML包格式错误:没有根元素");
}
List it = element.getChildren();
int len = it.size();
if (len == 0) {
throw new Exception("XML包格式错误:没有子元素");
}
map = new HashMap();
for (int i = 0; i < len; i++) {
Element subelement = (Element) it.get(i);
List itc = subelement.getChildren();
if (itc != null && itc.size() > 0) {
for (int n = 0; n < itc.size(); n++) {
Element sub = (Element) itc.get(n);
map.put(sub.getName().toLowerCase(), sub.getText());
}
}
map.put(subelement.getName().toLowerCase(), subelement.getText());
}
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* @Title: parseXmlToListMap
* @Description: xml转listmap
* @param @param xml
* @param @return
* @return List 返回类型
* @throws
*/
@SuppressWarnings("unchecked")
public static List> parseXmlToListMap(String xml) {
List> argMapList = new ArrayList>();
Map retMap = new HashMap();
try {
StringReader read = new StringReader(xml);
// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
InputSource source = new InputSource(read);
// 创建一个新的SAXBuilder
SAXBuilder sb = new SAXBuilder();
// 通过输入源构造一个Document
Document doc = (Document) sb.build(source);
Element root = doc.getRootElement();// 指向根节点
if(root != null){
List es = root.getChildren();
if (es != null && es.size() != 0) {
for (Element element : es) {
retMap.put(element.getName(), element.getText());
}
}
}
argMapList.add(retMap);
} catch (Exception e) {
e.printStackTrace();
}
return argMapList;
}
public static void main(String[] args) {
String xml = "12345678";
System.out.println(parseXmlToListMap(xml).get(0));
}
}