以下是hibernate的配置文件,我想读取connection.username,connection.url,connection.password和connection.driver_class的值,听说可以用dom4j可以处理,请问使用dom4j怎样读取,最好有实例代码。
<?xml version='1.0' encoding='UTF-8'?><hibernate-configuration><session-factory>
<property name="connection.username">jtuser</property>
<property name="connection.url">jdbc:microsoft:sqlserver://172.44.22.43:1433</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="myeclipse.connection.profile">172.22.11.43</property>
<property name="connection.password">jt123</property>
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
</session-factory></hibernate-configuration>

解决方案 »

  1.   

    jdk1.4以后都支持xml读取,下面是一个例子:
    package xmlreader;
    import java.util.*;
    import java.io.*;import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;import org.w3c.dom.*;import org.xml.sax.*;public class XmlPropertyReader {
        private static Map propertyMap = null;
        private static final String fileName = "/dbProperties.xml";
        private XmlPropertyReader() {};    public static String getProperty(String key) throws Exception {
            if (propertyMap == null) {
                propertyMap = loadProperties();
            }        if (propertyMap.containsKey(key)) {
                return (String) propertyMap.get(key);
            } else {
                throw new Exception("Property " + key + " does not exist");
            }
        }    private static Map loadProperties() throws Exception {
            Document doc = null;        try {
                DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                                          .newDocumentBuilder();            InputStream is = XmlPropertyReader.class.getResourceAsStream(
                        fileName);
                doc = builder.parse(is);
                //ServletUtility.printXML(doc);            return getMap(doc.getElementsByTagName("property"));        } catch (Exception ex) {
                throw ex;
                //logger.error(ex);
            }
        }    private static Map getMap(NodeList list) throws Exception {
            Map map = new HashMap();        if (list == null) {
                return map;
            }        for (int i = 0; i < list.getLength(); i++) {
                String key = null;
                String value = null;
                Node parentNode = list.item(i);            NamedNodeMap attributes = parentNode.getAttributes();            for (int j = 0; j < attributes.getLength(); j++) {
                    Node att = attributes.item(j);                if (att.getNodeName().equals("key")) {
                        key = att.getNodeValue();
                    } else if (att.getNodeName().equals("value")) {
                        value = att.getNodeValue();
                    } else {
                        throw new Exception(
                                "Attribute must be either key or value");
                    }
                }            if ((key == null) || (value == null)) {
                    throw new Exception("Either key or value is not present");
                }            map.put(key, value);
            }        return map;
        }
    }
      

  2.   

    org.apache.dom4j综合效率dom4j是最高的,并且非常容易使用,支持XPATH
      

  3.   

    http://www.dom4j.org/guide.html这是dom4j的guide 自己去看看吧代码自己写吧