import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.xpath.XPath; /** * @author 231800 */ public class XMLUtil { @SuppressWarnings( { "unchecked", "deprecation" }) // 读XML节点数据 public HashMap<String, String> readXMLDocForMap(String path) throws IOException, JDOMException { HashMap<String, String> map = new HashMap<String, String>(); String ROOT_PATH = this.getClass().getResource(path).getFile(); ROOT_PATH = URLDecoder.decode(ROOT_PATH); File fn = new File(ROOT_PATH); Document doc = null; // 建立构造器 SAXBuilder sb = new SAXBuilder(); // 判断文件是否存在 if (fn.exists()) { try { // 读入指定文件 doc = sb.build(new FileInputStream(fn)); } catch (Exception e) { System.out.println(e.getMessage()); } // 获得根结点 Element root = doc.getRootElement(); // 将根结点下的所有子节点放入List中 List list = root.getChildren(); for (int i = 0; i < list.size(); i++) { // 取得节点实例 Element item = (Element) list.get(i); List chileList = item.getChildren(); { for (int j = 0; j < chileList.size(); j++) { Element chileItem = (Element) chileList.get(j); map.put(chileItem.getName(), chileItem.getText()); } } } } else { System.out.println("Not found child element!"); } return map; } @SuppressWarnings( { "deprecation", "unchecked" }) public List readXMLDocForList(String path) throws IOException, JDOMException { ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>(); String ROOT_PATH = this.getClass().getResource(path).getFile(); ROOT_PATH = URLDecoder.decode(ROOT_PATH); File fn = new File(ROOT_PATH); Document doc = null; // 建立构造器 SAXBuilder sb = new SAXBuilder(); // 判断文件是否存在 if (fn.exists()) { try { // 读入指定文件 doc = sb.build(new FileInputStream(fn)); } catch (Exception e) { System.out.println(e.getMessage()); } // 获得根结点 Element root = doc.getRootElement(); // 将根结点下的所有子节点放入List中 List list = root.getChildren(); for (int i = 0; i < list.size(); i++) { // 取得节点实例 Element item = (Element) list.get(i); // 取得当前节点的子节点 List chileList = item.getChildren(); { for (int j = 0; j < chileList.size(); j++) { Element chileItem = (Element) chileList.get(j); HashMap<String, String> map = new HashMap<String, String>(); map.put(chileItem.getName(), chileItem.getText()); result.add(map); } } } } else { System.out.println("Not found child element!"); } return result; } @SuppressWarnings( { "deprecation", "unchecked" }) public List readXMLDocForList(String path, String str) throws IOException, JDOMException { String ROOT_PATH = this.getClass().getResource(path).getFile(); ROOT_PATH = URLDecoder.decode(ROOT_PATH); File fn = new File(ROOT_PATH); Document doc = null; // 建立构造器 SAXBuilder sb = new SAXBuilder(); // 判断文件是否存在 if (fn.exists()) { try { // 读入指定文件 doc = sb.build(new FileInputStream(fn)); } catch (Exception e) { System.out.println(e.getMessage()); } } Element driverElt = (Element) XPath.selectSingleNode(doc .getRootElement(), str); ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>(); // 将根结点下的所有子节点放入List中 List list = driverElt.getChildren(); for (int i = 0; i < list.size(); i++) { // 取得节点实例 Element item = (Element) list.get(i); // 取得当前节点的子节点 List chileList = item.getChildren(); HashMap<String, String> map = new HashMap<String, String>(); for (int j = 0; j < chileList.size(); j++) { Element chileItem = (Element) chileList.get(j); map.put(chileItem.getName(), chileItem.getText()); } result.add(map); } return result; } /** * @param args */ public static void main(String[] args) { try { XMLUtil j2x = new XMLUtil(); List list = j2x.readXMLDocForList("/data/defaultAccount.xml", "//accountlist/company_account");// 读取 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } catch (Exception e) { e.printStackTrace(); } } }