Dom4j是一个比较常用的chf@tsinghua.org.cn http://hiyachen.cublog.cn
package hiya;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class Dom4jTest {
private static String inDataFile = "c:\\emp. private static String inConfigFile = "c:\\dom4jConfig. private static String outFile = "c:\\student.
/**
* 解析 */
private void readDom4j(String filename){
//输入流对象
SAXReader reader = new SAXReader();
// Document document = null;
try {
document = reader.read(new File(filename));
} catch (DocumentException e) {
System.out.println(filename + "文件不存在!");
return;
}
//获得根节点元素
Element root = document.getRootElement();
//输出元素的个数
System.out.println("共有" + root.nodeCount() + "条数据");
//获得所有的下一级子节点
List list = root.elements();
//遍历子节点
for (Element row : list) {
List cols = row.elements();
for (Element col : cols) {
System.out.println(col.getName() + "=" + col.getStringValue());
}
System.out.println("------------------");
}
};
/**
* 解析 */
private void readDom4jConfig(String filename){
//输入流对象
SAXReader reader = new SAXReader();
//文档对象
Document document = null;
try {
document = reader.read(new File(filename));
} catch (DocumentException e) {
System.out.println("文件不存在");
return;
}
//获得根节点
Element root = document.getRootElement();
System.out.println("根标签:" + root.getName() + "
");
//获得所有子节点集合
List elements = root.elements();
getAllNode(elements, root.getName());
//遍历子节点
// for (Element e : elements) {
// System.out.println("子节点1:" + e.getName() + "
");
// if (e.getName().equals("class")) {
// //获得属性集合
// List arrtibutes = e.attributes();
// //遍历属性
// for (Attribute a : arrtibutes) {
// System.out.println(a.getName() + "=" + a.getValue() + " ");
// }
// //System.out.println("
");
// //获得所有子节点集合
// List elements2 = e.elements();
// //遍历子节点
// for (Element e2 : elements2) {
// System.out.println("子节点2:" + e2.getName() + "
");
// //获得属性集合
// List arrtibutes2 = e2.attributes();
// //遍历属性
// for (Attribute a : arrtibutes2) {
// System.out.println(a.getName() + "=" + a.getValue() + " ");
// }
// //System.out.println("
");
// }
// }
// }
System.out.close();
};
/**
* 通过xpath解析 */
private void readDom4jXpath(String filename){
//输入流
SAXReader reader = new SAXReader();
//文档对象
Document document = null;
try {
document = reader.read(new File(filename));
} catch (DocumentException e) {
e.printStackTrace();
return;
}
//用xpath直接定位到某个节点,返回这个节点的集合
List list = document.selectNodes("//hibernate-mapping/class/property");
//遍历节点
for (Node node : list) {
System.out.println(node.getName() + "
");
//返回这个节点name属性的值
String value = node.valueOf("@name");
System.out.println("name=" + value + "
");
}
//获得单个节点,多个同名节点只拿第一个
Node node = document.selectSingleNode("//class");
System.out.println(node.getName());
};
/**
* 利用dom4j创建和写入 */
private void createDOM4j(String outFile) {
try {
// 文件输出流
FileWriter fw = new FileWriter(outFile);
// 创建一个文档对象
Document document = DocumentHelper.createDocument();
// 加入根节点
Element root = document.addElement("hibernate-mapping");
// 加入子节点
Element e1 = root.addElement("class");
// 设置属性和属性值
e1.addAttribute("name", "com.Student");
e1.addAttribute("table", "student");
// 用方法链加入子节点和属性
Element e2 = e1.addElement("property").addAttribute("name", "sid")
.addAttribute("column", "s_id");
Element e3 = e1.addElement("property").addAttribute("name", "sname")
.addAttribute("column", "s_name");
// 生成 document.write(fw);
// 清除缓冲区
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private void getAllNode(List elements, String nodePath) {
String nodePathTmp = nodePath;
for (Element e : elements) {
nodePathTmp = nodePath + ":" + e.getName();
System.out.println("全节点:" + nodePathTmp + "
");
System.out.println("子节点:" + e.getName() + "
");
// 获得属性集合
List arrtibutes = e.attributes();
// 遍历属性
for (Attribute a : arrtibutes) {
System.out.println(a.getName() + "=" + a.getValue() + " ");
}
System.out.println("
");
List subElements = e.elements();
getAllNode(subElements, nodePathTmp);
}
}
/**
* @param args
*/
public static void main(String[] args) {
Dom4jTest dom4jTest = new Dom4jTest();
dom4jTest.createDOM4j(outFile);
dom4jTest.readDom4j(inDataFile);
dom4jTest.readDom4jConfig(inConfigFile);
dom4jTest.readDom4jXpath(inConfigFile);
}
}
emp.
dom4jConfig.