以下是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>
<?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>
解决方案 »
- 问个很傻的问题,32位CPU的服务器,可以使用超过4G的内存么?
- JDBC 中 statement.isClosed() 报错
- 各位帮帮我看看这个???????????????
- 环境变量CLASSPATH的值应该是什么?
- 一个swt中感觉比较难的问题,100分,近来就有,谢谢
- 各位大侠我想问一JLable 可不可以定意成数组呀!
- java里函数的参数怎么可以象C++里的枚举类型一样限制值的范围
- 扩展了JFrame写了一个程序,是否可以改写成applet形式,要注意哪些问题?
- 求助:如何在Java程序中启动系统的浏览器?谢谢了!!!
- java中库和包的区别是什么? import java.util.*;那个是包,那个是库
- 简单问题
- 问题集合
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;
}
}