最近做课程设计,我把dao的数据源配置在xml的文件中,就写了个用dom解析xml的一个通用类,写的不好,请大家多多指教,恶哈哈! import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * <p> * 利用Dom解析XML的一個通用类 <code>XML文件放在src的根目录下</code> * </p> * * @author 懶人-小何 * @version 2009-12-6 * @since 1.0 */ public class ParseXmlUtils { private final Map<String, String> xmlMap = new HashMap<String, String>();// 存放xml中元素 private final DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); private static ParseXmlUtils instance = null; private DocumentBuilder builder = null; private Document document = null; private Element root = null; private ParseXmlUtils() { } /** * <p> * 单例,获得该类的唯一实例 * </p> * * @return ParseXmlUtils 的一个实例 */ public synchronized static final ParseXmlUtils newInstance() { if (instance == null) instance = new ParseXmlUtils(); return instance; } /** * <p> * 解析xml,传入xml文件名 * </p> * * @param path * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public void parse(final String xmlName) throws ParserConfigurationException, SAXException, IOException { builder = factory.newDocumentBuilder(); document = builder.parse(ParseXmlUtils.class .getResourceAsStream(xmlName)); root = document.getDocumentElement(); parse(root, root.getAttribute("name")); } /** * <p> * 递归解析xml文件,把数据映射到map中 * </p> * * @param el * @param key */ private void parse(final Element el, final String key) { NodeList nodeList = el.getChildNodes(); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; NodeList enodeList = element.getChildNodes(); if (enodeList.getLength() == 1) { String key1 = key + ":" + element.getAttribute("name").trim(); String value = element.getTextContent().trim(); xmlMap.put(key1, value); // System.out.print("key = " + key1); // System.out.println(" : value = " + value); } else parse(element, key + ":" + element.getAttribute("name")); } } } /** * 获得xml映射的map对象 * * @return Map */ public Map<String, String> getXmlMap() { return xmlMap; } public static void main(String[] args) throws Exception { ParseXmlUtils utils = ParseXmlUtils.newInstance(); utils.parse("/jdbcTemplate.xml"); System.out.println(utils.getXmlMap().get("root:driver-class")); System.out.println(utils.getXmlMap().get("root:test:value1:value2")); } } 輸出: com.mysql.jdbc.Driver value1-value2 xml的內容: <?xml version="1.0" encoding="UTF-8"?> <jdbcTemplate name="root"> <property name="dialect">MySql</property> <property name="driver-class">com.mysql.jdbc.Driver</property> <property name="driver-url"> jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8 </property> <property name="login-name">root</property> <property name="login-password">123456</property> <property name="test"> <pro name="value1"> <param name="value2">value1-value2</param> </pro> <pro name="value3">value3</pro> </property> </jdbcTemplate> 缺陷:无法解析节点名,属性名一样的多子节点。 发现bug时间:2012-03-07日,杯具的一天。