/** *//** * 创建XML文件 * @author [email protected] * @param fileName * @date Jul 10, 2006 3:34:58 PM * @return rtn true or false */ public boolean createXMLFile(String fileName) { boolean rtn = false; // 使用DocumentHelper.createDocument方法建立一个文档实例 Document document = DocumentHelper.createDocument(); // 使用addElement方法方法创建根元素 Element catalogElement = document.addElement("catalog"); // 使用addComment方法方法向catalog元素添加注释 catalogElement.addComment("An XML cataog"); // 使用addProcessInstruction向catalog元素增加处理指令 catalogElement.addProcessingInstruction("target", "text"); // 使用addElement方法向catalog元素添加journal子元素 Element journalElement = catalogElement.addElement("journal"); // 使用addAttribute方法向journal元素添加title和publisher属性 journalElement.addAttribute("title", "XML Zone"); journalElement.addAttribute("publisher", "Willpower Co"); // 使用addElement方法向journal元素添加article子元素 Element articleElement = journalElement.addElement("article"); // 使用addAttribute方法向article元素添加level和date属性 articleElement.addAttribute("level", "Intermediate"); articleElement.addAttribute("date", "July-2006"); // 使用addElement方法向article元素添加title子元素 Element titleElement = articleElement.addElement("title"); // 使用setText方法设置title子元素的值 titleElement.setText("Dom4j Create XML Schema"); // 使用addElement方法向article元素添加authorElement子元素 Element authorElement = articleElement.addElement("author"); // 使用addElement方法向author元素添加firstName子元素 Element firstName = authorElement.addElement("fistname"); // 使用setText方法设置firstName子元素的值 firstName.setText("Yi"); // 使用addElement方法向author元素添加lastname子元素 Element lastName = authorElement.addElement("lastname"); // 使用setText方法设置lastName子元素的值 lastName.setText("Qiao"); XMLWriter output; //输出格式化 OutputFormat format = OutputFormat.createPrettyPrint(); try { output = new XMLWriter(new FileWriter(fileName), format); output.write(document); output.close(); rtn = true; } catch (IOException e) { e.printStackTrace(); } return rtn; }
<books> <!--This is a test for dom4j, holen, 2004.9.11--> <book show="no"> <title>Dom4j Tutorials</title> </book> <book show="no"> <title>Lucene Studing</title> </book> <book show="no"> <title>Lucene in Action</title> </book> <owner>O'Reilly</owner> </books>
---------------------
package com.holen.dom4j;
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;
/**
* @author Holen Chen
*/
public class Dom4jDemo {
public Dom4jDemo() {
}
/**
* 建立一个XML文档,文档名由输入参数决定
* @param filename 需建立的文件名
* @return 返回操作结果, 0表失败, 1表成功
*/
public int createXMLFile(String filename){
/** 返回操作结果, 0表失败, 1表成功 */
int returnValue = 0;
/** 建立document对象 */
Document document = DocumentHelper.createDocument();
/** 建立XML文档的根books */
Element booksElement = document.addElement("books");
/** 加入一行注释 */
booksElement.addComment("This is a test for dom4j, holen, 2004.9.11");
/** 加入第一个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("O'Reilly");
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;
}
/**
* 修改XML文件中内容,并另存为一个新文件
* 重点掌握dom4j中如何添加节点,修改节点,删除节点
* @param filename 修改对象文件
* @param newfilename 修改后另存为该文件
* @return 返回操作结果, 0表失败, 1表成功
*/
public int ModiXMLFile(String filename,String newfilename){