我的一个实际应用, 两个文件OUCode.java OUCodeData.java
package rook.util;import java.io.*;
import javax.xml.parsers.*;import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.IOException;
import org.w3c.dom.NamedNodeMap;import org.apache.xerces.dom.*;
import org.apache.xml.serialize.*;
import org.apache.xerces.parsers.DOMParser;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class OUCode {
private DocumentBuilder parser = null;
private Document xmldoc = null;

//构造函数, 创建空的XML内容
public OUCode() {
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
try {
this.parser = dbfactory.newDocumentBuilder();
this.addRootNode();
} catch (Exception exp) {
this.parser = null;
this.xmldoc = null;
exp.printStackTrace();
}
} //装载文件, 如果文件不存在或者出错, 创建空的XML内容
public void load(String strOUCodeFile) 
throws Exception {
if ( this.parser == null ) {
throw new Exception("Parser initialize error!");
}
try {
this.xmldoc = this.parser.parse(strOUCodeFile);
} catch (Exception e) {
e.printStackTrace();
this.xmldoc = this.parser.newDocument();
}
} //保存当前的XML内容到文件
public void save(String strOUCodeFile)
throws Exception {
if ( this.parser == null ) {
throw new Exception("Parser initialize error!");
}
File outputFile = new File(strOUCodeFile);
OutputFormat outputFormat = new OutputFormat("XML","gb2312",true);
FileWriter fileWriter=new FileWriter(outputFile);
XMLSerializer xmlSerializer=new XMLSerializer(fileWriter,outputFormat);
xmlSerializer.asDOMSerializer();
xmlSerializer.serialize(xmldoc.getDocumentElement());
fileWriter.close();
} //添加根节点
private void addRootNode() 
throws Exception{
this.xmldoc = this.parser.parse(new ByteArrayInputStream(new String("<?xml version=\"1.0\" ?><AreaCode/>").getBytes()));
}

//添加一级节点
public void addNode(String strName, String strValue) {
Element OUCode = (Element)this.getNodeByName(strName);
if ( OUCode != null ) {
OUCode.setAttribute("Value", strValue);
} else {
OUCode = xmldoc.createElement("OUCode");
OUCode.setAttribute("Name", strName);
OUCode.setAttribute("Value", strValue);
OUCode.setAttribute("ChildNode", "0");
xmldoc.getDocumentElement().appendChild(OUCode);
}
} //添加二级节点
public boolean addChildNode(String strName, String strValue, String strParentName) {
Element ChildOUCode = (Element)this.getNodeByName(strName);

if ( ChildOUCode != null ) {
return false;
} ChildOUCode = (Element)this.getChildNodeByName(strName);
if ( ChildOUCode != null && !this.getNodeAttributeValue(ChildOUCode.getParentNode(), "Name").equals(strParentName) ) {
return false;
} Element OUCode = (Element)this.getNodeByName(strParentName);
if ( OUCode == null ) {
return false;
} else {
OUCode.setAttribute("ChildNode", "1");
if ( ChildOUCode != null ) {
ChildOUCode.setAttribute("Value", strValue);
} else {
ChildOUCode = this.xmldoc.createElement("Child-OUCode");
ChildOUCode.setAttribute("Name", strName);
ChildOUCode.setAttribute("Value", strValue);
OUCode.appendChild(ChildOUCode);
}
}
return true;
} //移除节点
public void removeNodeByName(String strName) {
Node node = this.getNodeByName(strName);
if ( node == null ) {
node = this.getChildNodeByName(strName);
}
if ( node != null ) {
Element parent = (Element)node.getParentNode();
parent.removeChild(node);
if ( !parent.hasChildNodes() ) {
parent.setAttribute("ChildNode", "0");
}
}
}

//提取所有OUCode列表
public OUCodeData[] getAllNodes() {
OUCodeData[] data = new OUCodeData[0];
OUCodeData[] temp;
NodeList nlist = xmldoc.getElementsByTagName("OUCode");
Element node = null; String strName;
String strValue;

for ( int i = 0; i < nlist.getLength(); i++ ) {
node = (Element)nlist.item(i);
temp = new OUCodeData[data.length+1];
if ( data.length > 0 ) {
System.arraycopy(data, 0, temp, 0, data.length);
}
strName = this.getNodeAttributeValue(node, "Name");
strValue = this.getNodeAttributeValue(node, "Value");

temp[data.length] = new OUCodeData(false, strName, strValue);
data = temp;
if ( node.hasChildNodes() ) {
NodeList sublist = node.getElementsByTagName("Child-OUCode");
for ( int j = 0; j < sublist.getLength(); j++ ) {
node = (Element)sublist.item(j);
temp = new OUCodeData[data.length+1];
System.arraycopy(data, 0, temp, 0, data.length);

strName = this.getNodeAttributeValue(node, "Name");
strValue = this.getNodeAttributeValue(node, "Value");

temp[data.length] = new OUCodeData(true, strName, strValue);
data = temp;
}
}
}
return data;
} //通过节点的Name属性查找节点
private Node getNodeByName(String strName) {
NodeList nlist = xmldoc.getElementsByTagName("OUCode");
Node node = null;
for ( int i = 0; i < nlist.getLength(); i++ ) {
if ( strName.equals(this.getNodeAttributeValue(nlist.item(i), "Name")) ) {
node = nlist.item(i);
break;
}
}
return node;
}

//通过节点的Name属性查找子节点
private Node getChildNodeByName(String strName) {
NodeList nlist = xmldoc.getElementsByTagName("Child-OUCode");
Node node = null;
for ( int i = 0; i < nlist.getLength(); i++ ) {
if ( strName.equals(this.getNodeAttributeValue(nlist.item(i), "Name")) ) {
node = nlist.item(i);
break;
}
}
return node;
} private String getNodeAttributeValue(Node node, String strName) {
NamedNodeMap nmap = node.getAttributes();

Node tmpnode = nmap.getNamedItem(strName);
String rtStr = "";
if ( tmpnode != null ) {
rtStr = tmpnode.getNodeValue().trim();
}
//System.out.println(strName + "=" + rtStr);
return rtStr;
} public static void main(String[] args) {
OUCode obj = new OUCode();
try {
obj.addNode("Test","Value");
obj.addNode("Test1","Value1");
obj.addChildNode("Just","Value", "Test");
obj.addChildNode("Just1","Value1", "Test");
obj.addChildNode("Just2","Value", "Test1");
obj.addChildNode("Just2","Value2", "Test1");
obj.save("H:\\XSchool\\MSWeb\\EPower\\xmlDoc\\result1.xml");
//obj.load("H:\\XSchool\\MSWeb\\EPower\\xmlDoc\\result1.xml");
//obj.save("H:\\XSchool\\MSWeb\\EPower\\xmlDoc\\result3.xml");
OUCodeData[] data = obj.getAllNodes();
for ( int i = 0; i < data.length; i++ ) {
System.out.println("IsChild = "+data[i].IsChild+"\tName = "+data[i].Name+"\tValue = "+data[i].Value);
}
obj.removeNodeByName("Just2");
obj.save("H:\\XSchool\\MSWeb\\EPower\\xmlDoc\\result2.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
}package rook.util;public class OUCodeData {
public boolean IsChild = false;
public String Name = "";
public String Value = "";
public OUCodeData() {
}
public OUCodeData(boolean isChild, String name, String value) {
this.IsChild = isChild;
this.Name = name;
this.Value = value;
}
}