dom4j学习笔记(一)

首先下载dom4j.jar,jaxen-1.1.1.jar包 http://sourceforge.net/projects/dom4j
(1)使用dom4j创建xml文件
package com.wyq.ajaxServlet;

import java.io.File;
import java.io.FileWriter;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

public class Dom4jDome {
 /*
  * 创建xml文档
  * */
 public static void main(String args[]){
  String fileName="c:/text.xml";
        //建立document对象,用来操作xml文件
  Document document=DocumentHelper.createDocument();
  //建立根节点
  Element booksElement=document.addElement("books");
  //加入一行注释
  booksElement.addComment("This is a test for dom4j, wyq");
  //添加一个book节点
  Element bookElement=booksElement.addElement("book");
  //添加属性内容
  bookElement.addAttribute("show","yes");
  //添加文本节点
  Element titleElement=bookElement.addElement("title");
  //添加文本内容
  titleElement.setText("ajax in action");
  try{
  XMLWriter writer=new XMLWriter(new FileWriter(new File(fileName)));
  writer.write(document);
  writer.close();
  }catch(Exception e){
   e.printStackTrace();
  }
  /*
   * 如果结合ajax返回的是xml,只需加上如下代码
   * String strReturn=booksElement.asXML();
   * out.print(strReturn);
   * */
 }
}
(2)、使用dom4j修改节点属性(内容)
public void modifyXMLFile(){
  String oldStr="c:/text.xml";
  String newStr="c:/text1.xml";
  Document document=null;
  try{
   //用来读取xml文档
   SAXReader saxReader=new SAXReader();
   //读取xml文档
    document=saxReader.read(new File(oldStr));
   //用xpath查找节点book的属性
   List list=document.selectNodes("/books/book/@show");
   //如果修改节点的内容,用xpath查找节点book的属性
   //List list=document.selectNodes("/books/book/title");
   Iterator iter=list.iterator();
   while(iter.hasNext()){
    Attribute attribute=(Attribute)iter.next();
   //这里要变成Element element=(Element)iter.next();
   //设置相应的内容
   //element.setText("xxx");
    if(attribute.getValue().equals("yes")){
     attribute.setValue("no");
    }
   }
  }catch(Exception e){
   e.printStackTrace();
  }
  try{
   XMLWriter writer=new XMLWriter(new FileWriter(new File(newStr)));
   writer.write(document);
   writer.close();
  }catch(Exception ex){
   ex.printStackTrace();
  }
 }
}
(3)、删除节点
public void removeNode(){
  String oldStr="c:/text.xml";
  String newStr="c:/text1.xml";
  Document document=null;
  try{
   //用来读取xml文档
   SAXReader saxReader=new SAXReader();
   //读取xml文档
    document=saxReader.read(new File(oldStr));
   //用xpath查找对象
   List list=document.selectNodes("/books/book");
   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("ajax in action")){
      bookElement.remove(titleElement);
     }
    }
   }
  }catch(Exception e){
   e.printStackTrace();
  }
  try{
   XMLWriter writer=new XMLWriter(new FileWriter(new File(newStr)));
   writer.write(document);
   writer.close();
  }catch(Exception ex){
   ex.printStackTrace();
  }
 }

你可能感兴趣的:(dom4j)