项目截图:注意导入红色方框内的两个jar包。
三个函数,一个用于创建xml文件,一个用于修改xml文件,一个用于格式化xml文件。
import java.io.File;import java.io.FileWriter;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.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;public class Dom4jXML { public static int createXMLFile(String filename){ /*返回操作结果*/ int returnValue = 0; /*建立document对象*/ Document document = DocumentHelper.createDocument(); /*建立XML文档的根books*/ Element booksElement = document.addElement("books"); /*加入一行注释*/ booksElement.addComment("This is a test for dom4j, ZHe, 2012.11.26"); /*加入第一个book节点*/ Element bookElement = booksElement.addElement("book"); /*加入show属性内容*/ bookElement.addAttribute("show", "yes"); /*加入title节点*/ Element titleElement = bookElement.addElement("title"); /*为title设置内容*/ titleElement.setText("Dom4j Tutorials"); /*类似的完成后两个book*/ bookElement = booksElement.addElement("book"); bookElement.addAttribute("show", "yes"); titleElement = bookElement.addElement("title"); titleElement.setText("Lucene Studing"); bookElement = booksElement.addElement("book"); bookElement.addAttribute("show", "no"); titleElement = bookElement.addElement("title"); titleElement.setText("Lucene in Action"); /*加入owner节点*/ Element ownerElement = booksElement.addElement("owner"); ownerElement.setText("Zhang He"); try{ /*将document中的内容写入文件中*/ XMLWriter writer = new XMLWriter(new FileWriter(new File(filename))); writer.write(document); writer.close(); /*执行成功,返回1*/ returnValue = 1; }catch(Exception ex){ ex.printStackTrace(); } return returnValue; } public static int modifyXMLFile(String filename, String newfilename){ int returnValue = 0; 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项内容改为CheeseZH * 并在owner节点中加入date节点,date节点的内容为2012-11-27 * 还要为date节点添加一个属性type */ list = document.selectNodes("books/owner"); iter = list.iterator(); if(iter.hasNext()){ Element ownerElement = (Element)iter.next(); ownerElement.setText("CheeseZH"); Element dateElement = ownerElement.addElement("date"); dateElement.setText("2012-11-27"); dateElement.addAttribute("type", "Gregoran calendar"); } /*修改内容之三:若title内容为Dom4j 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("Dom4j Tutorials")){ bookElement.remove(titleElement); } } } try{ /*将document中的内容写入文件中*/ XMLWriter writer = new XMLWriter(new FileWriter(new File(newfilename))); writer.write(document); writer.close(); /*执行成功,返回1*/ returnValue = 1; }catch(Exception ex){ ex.printStackTrace(); } }catch(Exception ex){ ex.printStackTrace(); } return returnValue; } public static int formatXMLFile(String filename){ int returnValue = 0; try{ SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new File(filename)); XMLWriter writer = null; /*格式化输出,类似IE浏览器一样*/ OutputFormat format = OutputFormat.createPrettyPrint(); /*指定XML编码*/ format.setEncoding("GB2312"); writer = new XMLWriter(new FileWriter(new File(filename)),format); writer.write(document); writer.close(); /*执行成功,返回1*/ returnValue = 1; }catch(Exception ex){ } return returnValue; } public static void main(String[] args){ createXMLFile("zhanghe.xml"); modifyXMLFile("zhanghe.xml","CheeseZH.xml"); formatXMLFile("zhanghe.xml"); }}
1.创建Document对象(xml对象),用addElement添加根节点(Element对象);
2.Element对象三个重要方法:
addComment:添加注释
addAttribute:添加属性
addElement:添加子元素
3.XMLWriter用于生成物理文件,默认使用OutputFormat类的createCompactFormat(),可以使用createPrettyPrint()
4.List list = document.selectNodes("/books/book");是通过xpath查找相应内容
关于Xpath语法,可以参考:
5.setValue(),setText()可以修改结点内容,remove()可以删除结点或者属性
6.OutputFormat format = OutputFormat.createPrettyPrint();指定了格式化方式为缩进式,而非紧凑式;
format.setEncoding("GB2312");用于指定编码
之后的XMLWriter生成物理文件时,多了一个OutputFormat对象作为参数