Dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections Framework and with full support for DOM, SAX and JAXP.
public class DOM4jParse {
public DOM4jParse() {
}
public boolean createXMLFile(String filename) {
boolean flag = false;
/** 建立document对象 */
Document document = DocumentHelper.createDocument();
/** 建立XML文档的根books */
Element booksElement = document.addElement("books");
/** 加入一行注释 */
booksElement.addComment("This is a test for dom4j, ivan, 2008.11.28");
/** 加入第一个book节点 */
Element bookElement = booksElement.addElement("book");
/** 加入show参数内容 */
bookElement.addAttribute("show", "yes");
/** 加入title节点 */
Element titleElement = bookElement.addElement("title");
/** 为title设置内容 */
titleElement.setText("EJB Tutorials");
/** 类似的完成后两个book */
bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "yes");
titleElement = bookElement.addElement("title");
titleElement.setText("Ajax Studing");
bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "no");
titleElement = bookElement.addElement("title");
titleElement.setText("IBatis in Action");
/** 加入owner节点 */
Element ownerElement = booksElement.addElement("owner");
ownerElement.setText("O'Reilly");
try {
/** 将document中的内容写入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document);
writer.close();
/** 执行成功,需返回1 */
flag = true;
} catch (Exception ex) {
ex.printStackTrace();
}
return flag;
}
/**
* 修改XML文件中内容,并另存为一个新文件 重点掌握dom4j中如何添加节点,修改节点,删除节点
*
* @param filename
* 修改对象文件
* @param newfilename
* 修改后另存为该文件
* @return 返回操作结果, 0表失败, 1表成功
*/
public boolean ModiXMLFile(String filename, String newfilename) {
boolean flag = false;
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(filename));
/** 修改内容之一: 如果book节点中show参数的内容为yes,则修改成no */
/** 先用xpath查找对象 */
List list = document.selectNodes("/books/book/@show");
Iterator iter = list.iterator();
while (iter.hasNext()) {
Attribute attribute = (Attribute) iter.next();
if (attribute.getValue().equals("yes")) {
attribute.setValue("no");
}
}
/**
* 修改内容之二: 把owner项内容改为Tshinghua
* 并在owner节点中加入date节点,date节点的内容为2004-09-11,还为date节点添加一个参数type
*/
list = document.selectNodes("/books/owner");
iter = list.iterator();
if (iter.hasNext()) {
Element ownerElement = (Element) iter.next();
ownerElement.setText("Tshinghua");
Element dateElement = ownerElement.addElement("date");
dateElement.setText("2004-09-11");
dateElement.addAttribute("type", "Gregorian calendar");
}
/** 修改内容之三: 若title内容为EJB Tutorials,则删除该节点 */
list = document.selectNodes("/books/book");
iter = list.iterator();
while (iter.hasNext()) {
Element bookElement = (Element) iter.next();
Iterator iterator = bookElement.elementIterator("title");
while (iterator.hasNext()) {
Element titleElement = (Element) iterator.next();
if (titleElement.getText().equals("EJB Tutorials")) {
bookElement.remove(titleElement);
}
}
}
try {
/** 将document中的内容写入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File(
newfilename)));
writer.write(document);
writer.close();
/** 执行成功,需返回1 */
flag = true;
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return flag;
}
/**
* 格式化XML文档,并解决中文问题
*
* @param filename
* @return
*/
public boolean formatXMLFile(String filename) {
boolean flag = false;
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(filename));
XMLWriter output = null;
/** 格式化输出,类型IE浏览一样 */
OutputFormat format = OutputFormat.createPrettyPrint();
/** 指定XML字符集编码 */
format.setEncoding("GBK");
output = new XMLWriter(new FileWriter(new File(filename)), format);
output.write(document);
output.close();
/** 执行成功,需返回1 */
flag = false;
} catch (Exception ex) {
ex.printStackTrace();
}
return flag;
}
public static void main(String[] args) {
DOM4jParse temp = new DOM4jParse();
System.out.println(temp.createXMLFile("holen.xml"));
// System.out.println(temp.ModiXMLFile("holen.xml","holen2.xml"));
System.out.println(temp.formatXMLFile("holen.xml"));
}
}
<?xml version="1.0" encoding="GBK"?>
<books>
<!--This is a test for dom4j, ivan, 2008.11.28-->
<book show="yes">
<title>EJB Tutorials</title>
</book>
<book show="yes">
<title>Ajax Studing</title>
</book>
<book show="no">
<title>IBatis in Action</title>
</book>
<owner>O'Reilly</owner>
</books>