package action.communication;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* 使用dom4j操作XML文件的工具类
*
* @author Administrater
*
*/
public class XMLUtil {
/**
* 在指定路径下创建一个xml文件
*
* @param filePath
* 路径,如C:\test.xml
*/
public static void createXMLFile(String filePath) {
File file = new File(filePath);
// 判断该文件是否存在,不存在则创建
if (!file.exists()) {
try {
file.createNewFile();
//System.out.println("===");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Document document = DocumentHelper.createDocument();// 建立document对象,用来操作xml文件
Element booksElement = document.addElement("books");// 建立根节点
booksElement.addComment("This is a test for dom4j ");// 加入一行注释
Element bookElement = booksElement.addElement("book");// 添加一个book节点
bookElement.addAttribute("show", "yes");// 添加属性内容
Element titleElement = bookElement.addElement("title");// 添加文本节点
titleElement.setText("ajax in action");// 添加文本内容
try {
XMLWriter writer = new XMLWriter(new FileWriter(file));
writer.write(document);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 修改节点的属性
public static void modifyXMLFileNodeAttr() {
String oldStr = "c:/test.xml";
Document document = null;
// 修改节点的属性
try {
SAXReader saxReader = new SAXReader(); // 用来读取xml文档
document = saxReader.read(new File(oldStr)); // 读取xml文档
List list = document.selectNodes("/books/book/@show");// 用xpath查找节点book的属性
System.out.println(list);
Iterator iter = list.iterator();
while (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals("yes")) {
attribute.setValue("no");
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
XMLWriter writer = new XMLWriter(new FileWriter(new File(oldStr)));
writer.write(document);
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 修改节点的内容
public static void modifyXMLFileNodeText() {
String oldStr = "c:/test.xml";
Document document = null;
// 修改节点的内容
try {
SAXReader saxReader = new SAXReader(); // 用来读取xml文档
document = saxReader.read(new File(oldStr)); // 读取xml文档
List list = document.selectNodes("/books/book/title");// 用xpath查找节点book的内容
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element element = (Element) iter.next();
element.setText("哈哈哈");// 设置相应的内容
}
} catch (Exception e) {
e.printStackTrace();
}
try {
XMLWriter writer = new XMLWriter(new FileWriter(new File(oldStr)));
writer.write(document);
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
//删除某节点
public static void removeNode() {
String oldStr = "c:/test.xml";
Document document = null;
try {
SAXReader saxReader = new SAXReader();// 用来读取xml文档
document = saxReader.read(new File(oldStr));// 读取xml文档
List list = document.selectNodes("/books/book");// 用xpath查找对象
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element bookElement = (Element) iter.next();
// 创建迭代器,用来查找要删除的节点,迭代器相当于指针,指向book下所有的title节点
Iterator iterator = bookElement.elementIterator("title");
while (iterator.hasNext()) {
Element titleElement = (Element) iterator.next();
if (titleElement.getText().equals("哈哈哈")) {
bookElement.remove(titleElement);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
XMLWriter writer = new XMLWriter(new FileWriter(new File(oldStr)));
writer.write(document);
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
//XMLUtil.createXMLFile("c:/test.xml");
//XMLUtil.modifyXMLFileNodeAttr();
//XMLUtil.modifyXMLFileNodeText();
//XMLUtil.removeNode();
}
}
转载处不详