方式1:
package jp.co.Cheng;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlPropertyReader {
private static Map<String,String> propertyMap = null;
private static final String fileName = "dbProperties.xml";
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<String,String> 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) {
ex.printStackTrace();
throw ex;
//logger.error(ex);
}
}
private static Map<String,String> getMap(NodeList list) throws Exception {
Map<String,String> map = new HashMap<String,String>();
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();
Node att = attributes.item(0);
if (att.getNodeName().equals("name")){
key = att.getNodeValue();
Node childNode = parentNode.getFirstChild();
value = childNode.getNodeValue();
}
// NamedNodeMap attributes = parentNode.getAttributes();
// for (int j = 0; j < attributes.getLength(); j++) {
// Node att = attributes.item(j);
//
// if (att.getNodeName().equals("name")) {
// key = att.getNodeValue();
// Node childNode = att.getFirstChild();
//
// } 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;
}
}
xml样式:
<?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>
方式2:
public class LoadXml ...{
/** *//**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception ...{
// TODO Auto-generated method stub
//从 XML 文档获取 DOM 文档实例。使用此类,应用程序员可以从 XML 获取一个Document
DocumentBuilder db=DocumentBuilderFactory.newInstance().newDocumentBuilder();
FileInputStream in=new FileInputStream("F:/wrokspace/learnspring/src/ch7/111.xml");
Document document=db.parse(in);
NodeList nl=document.getElementsByTagName("a");
showElem(nl);
in.close();
}
/** *//**
* 通过递归得到所有子节点
* @param nl
*/
public static void showElem(NodeList nl)...{
for(int i=0;i<nl.getLength();i++)...{
Node n=nl.item(i);
if(n.hasChildNodes())...{
System.out.print("<"+n.getNodeName()+">");
//递归
showElem(n.getChildNodes());
System.out.print("</"+n.getNodeName()+">");
}else...{
//判断是不是文本
if(n.getNodeType()==Node.TEXT_NODE)...{
System.out.print(n.getNodeValue());
}else...{
System.out.print("<"+n.getNodeName()+">");
System.out.print("</"+n.getNodeName()+">");
}
break;
}
}
}
}
xml文档的格式:
<aa><a><t1><tt1>32324</tt1></t1><c1>cccc</c1></a><a><t1><tt1></tt1></t1><c1>cccc</c1></a></aa
文章出处:http://www.diybl.com/course/3_program/java/javashl/2008520/117123.html