package org.sws.utils;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.sws.model.Server;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ConfigurationUtil {
private String conf = "configuration.xml";
//Document是XML在内存中的一个镜像,获取了Document就可以通过对内存的操作来实现对XML的操作。
private Document doc = null ;
public Server getConfig(){
//从DocumentBuilderFactory中获取一个DocumentBuilder的实例
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
//使用DocumentBuilder来产生一个Document实例
doc = db.parse(new File(conf));
//Element代表XML中的一个标签对,可用于获取属性值
Element element = doc.getDocumentElement();
//获取该Element的标签名
System.out.println("Root Element : "+element.getTagName());
//通过标签名来获取多个节点
NodeList nodeList = doc.getElementsByTagName("Service");
System.out.println("NodeList Length : "+nodeList.getLength());
Node node = nodeList.item(0);
System.out.println("First Node : "+node.getNodeName());
//通过Agetttributes()方法来获取一个NamedNodeMap实例,该实例包含标签属性值
NamedNodeMap attrs = node.getAttributes();
for (int i=0; i<attrs.getLength(); i++){
Node attr = attrs.item(i);
System.out.println(attr.getNodeName()+" : "+attr.getNodeValue());
}
NodeList childNodes = node.getChildNodes();
for (int i=0; i<childNodes.getLength(); i++){
Node child = childNodes.item(i);
//当子节点是一个Element时才能获取该元素的标签名和属性值
if (child instanceof Element)
{
System.out.println(child.getNodeName()
+" : "+child.getFirstChild().getNodeValue());
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null ;
}
}
XML文件:
<?xml version='1.0' encoding='utf-8'?>
<Server>
<Service name="SampleWebServer">
<Listen>8080</Listen>
<Protocol>HTTP/1.1</Protocol>
<Host>localhost</Host>
<Root>/html</Root>
</Service>
<Service name="SampleWebServer1">
<Listen>8081</Listen>
<Protocol>HTTP/1.1</Protocol>
<Host>localhost</Host>
<Root>/html</Root>
</Service>
</Server>