1 package com.holen.dom4j; 2 import java.io.File; 3 import java.io.FileWriter; 4 import java.util.Iterator; 5 import java.util.List; 6 import org.dom4j.Attribute; 7 import org.dom4j.Document; 8 import org.dom4j.DocumentHelper; 9 import org.dom4j.Element; 10 import org.dom4j.io.OutputFormat; 11 import org.dom4j.io.SAXReader; 12 import org.dom4j.io.XMLWriter; 13 /** 14 * @author Holen Chen 15 */ 16 public class Dom4jDemo { 17 18 public Dom4jDemo() { 19 } 20 21 /** 22 * 建立一个XML文档,文档名由输入参数决定 23 * @param filename 需建立的文件名 24 * @return 返回操作结果, 0表失败, 1表成功 25 */ 26 public int createXMLFile(String filename){ 27 /** 返回操作结果, 0表失败, 1表成功 */ 28 int returnValue = 0; 29 /** 建立document对象 */ 30 Document document = DocumentHelper.createDocument(); 31 /** 建立XML文档的根books */ 32 Element booksElement = document.addElement("books"); 33 /** 加入一行注释 */ 34 booksElement.addComment("This is a test for dom4j, holen, 2004.9.11"); 35 /** 加入第一个book节点 */ 36 Element bookElement = booksElement.addElement("book"); 37 /** 加入show参数内容 */ 38 bookElement.addAttribute("show","yes"); 39 /** 加入title节点 */ 40 Element titleElement = bookElement.addElement("title"); 41 /** 为title设置内容 */ 42 titleElement.setText("Dom4j Tutorials"); 43 44 /** 类似的完成后两个book */ 45 bookElement = booksElement.addElement("book"); 46 bookElement.addAttribute("show","yes"); 47 titleElement = bookElement.addElement("title"); 48 titleElement.setText("Lucene Studing"); 49 bookElement = booksElement.addElement("book"); 50 bookElement.addAttribute("show","no"); 51 titleElement = bookElement.addElement("title"); 52 titleElement.setText("Lucene in Action"); 53 54 /** 加入owner节点 */ 55 Element ownerElement = booksElement.addElement("owner"); 56 ownerElement.setText("O'Reilly"); 57 58 try{ 59 /** 将document中的内容写入文件中 */ 60 XMLWriter writer = new XMLWriter(new FileWriter(new File(filename))); 61 writer.write(document); 62 writer.close(); 63 /** 执行成功,需返回1 */ 64 returnValue = 1; 65 }catch(Exception ex){ 66 ex.printStackTrace(); 67 } 68 69 return returnValue; 70 } 71 72 /** 73 * 修改XML文件中内容,并另存为一个新文件 74 * 重点掌握dom4j中如何添加节点,修改节点,删除节点 75 * @param filename 修改对象文件 76 * @param newfilename 修改后另存为该文件 77 * @return 返回操作结果, 0表失败, 1表成功 78 */ 79 public int ModiXMLFile(String filename,String newfilename){ 80 int returnValue = 0; 81 try{ 82 SAXReader saxReader = new SAXReader(); 83 Document document = saxReader.read(new File(filename)); 84 /** 修改内容之一:如果book节点中show参数的内容为yes,则修改成no */ 85 /** 先用xpath查找对象 */ 86 List list = document.selectNodes("/books/book/@show" ); 87 Iterator iter = list.iterator(); 88 while(iter.hasNext()){ 89 Attribute attribute = (Attribute)iter.next(); 90 if(attribute.getValue().equals("yes")){ 91 attribute.setValue("no"); 92 } 93 } 94 95 /** 96 * 修改内容之二:把owner项内容改为Tshinghua 97 * 并在owner节点中加入date节点,date节点的内容为2004-09-11,还为date节点添加一个参数type 98 */ 99 list = document.selectNodes("/books/owner" ); 100 iter = list.iterator(); 101 if(iter.hasNext()){ 102 Element ownerElement = (Element)iter.next(); 103 ownerElement.setText("Tshinghua"); 104 Element dateElement = ownerElement.addElement("date"); 105 dateElement.setText("2004-09-11"); 106 dateElement.addAttribute("type","Gregorian calendar"); 107 } 108 109 /** 修改内容之三:若title内容为Dom4j Tutorials,则删除该节点 */ 110 list = document.selectNodes("/books/book"); 111 iter = list.iterator(); 112 while(iter.hasNext()){ 113 Element bookElement = (Element)iter.next(); 114 Iterator iterator = bookElement.elementIterator("title"); 115 while(iterator.hasNext()){ 116 Element titleElement=(Element)iterator.next(); 117 if(titleElement.getText().equals("Dom4j Tutorials")){ 118 bookElement.remove(titleElement); 119 } 120 } 121 } 122 123 try{ 124 /** 将document中的内容写入文件中 */ 125 XMLWriter writer = new XMLWriter(new FileWriter(new File(newfilename))); 126 writer.write(document); 127 writer.close(); 128 /** 执行成功,需返回1 */ 129 returnValue = 1; 130 }catch(Exception ex){ 131 ex.printStackTrace(); 132 } 133 134 }catch(Exception ex){ 135 ex.printStackTrace(); 136 } 137 return returnValue; 138 } 139 140 /** 141 * 格式化XML文档,并解决中文问题 142 * @param filename 143 * @return 144 */ 145 public int formatXMLFile(String filename){ 146 int returnValue = 0; 147 try{ 148 SAXReader saxReader = new SAXReader(); 149 Document document = saxReader.read(new File(filename)); 150 XMLWriter output = null; 151 /** 格式化输出,类型IE浏览一样 */ 152 OutputFormat format = OutputFormat.createPrettyPrint(); 153 /** 指定XML字符集编码 */ 154 format.setEncoding("GBK"); 155 output = new XMLWriter(new FileWriter(new File(filename)),format); 156 output.write(document); 157 output.close(); 158 /** 执行成功,需返回1 */ 159 returnValue = 1; 160 }catch(Exception ex){ 161 ex.printStackTrace(); 162 } 163 return returnValue; 164 } 165 public static void main(String[] args) { 166 Dom4jDemo temp = new Dom4jDemo(); 167 System.out.println(temp.createXMLFile("d://holen.xml")); 168 System.out.println(temp.ModiXMLFile("d://holen.xml","d://holen2.xml")); 169 System.out.println(temp.formatXMLFile("d://holen2.xml")); 170 } 171 }