dom4j解析XML

来源:互联网 发布:js decodeuri utf8 编辑:程序博客网 时间:2024/05/21 04:02
package dom4j;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;


import org.dom4j.Document;
import org.dom4j.DocumentException;
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 Dom4jTest {


/**
* @author wangm
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
run5();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取第二书的作者
* @throws DocumentException
*/
public static void run1() throws DocumentException{
//获取 解析器对象
SAXReader reader = new SAXReader();
//解析XML,返回document对象
Document document = reader.read("src/book2.xml");
//获取根结点
Element root = document.getRootElement();
//获取书的结点
List<Element> books = root.elements("书");
//获取第二本书作者
Element book2 = books.get(1);
Element author2 = book2.element("作者");
//获取文本内容
System.out.println(author2.getText());

}
/**
* 在第二本书下添加子结点
* @throws DocumentException
* @throws IOException
*/
public static void run2() throws DocumentException, IOException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book2.xml");
Element root = document.getRootElement();
List<Element> books = root.elements("书");
Element book2 = books.get(1);
//在第二本书下添加子结点
book2.addElement("cat").setText("I'm a cat");
//创建漂亮的格式
//OutputFormat format = OutputFormat.createPrettyPrint(); 
//创建紧凑的格式 
OutputFormat format = OutputFormat.createCompactFormat();
//设置输出编码格式
format.setEncoding("UTF-8");
//回写
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book2.xml"),format);
writer.write(document);
writer.close();
}

/**
* 在第二本书的作者之前添加结点
* @throws DocumentException 
* @throws IOException 
*/
public static void run3() throws DocumentException, IOException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book2.xml");
Element root = document.getRootElement();
Element book2 =(Element)root.elements("书").get(1);
//获取所有书下的子结点,返回list集合
List<Element> list = book2.elements();
//创建元素对象DocumentHelper.createElement("狗")
Element dog = DocumentHelper.createElement("狗");
dog.setText("dog");
//list.add(index.Element);
list.add(1,dog);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book2.xml"),format);
writer.write(document);
writer.close();
}


/**
* 删除指定的结点
* @throws DocumentException 
* @throws IOException 

*/
public static void run4() throws DocumentException, IOException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book2.xml");
Element root = document.getRootElement();
Element book2 =(Element)root.elements("书").get(1);
Element cat = book2.element("cat");
book2.remove(cat);
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book2.xml"),format);
writer.write(document);
writer.close();

}
/**
* 修改 节点内容 
* @throws DocumentException
* @throws IOException 
*/
public static void run5() throws DocumentException, IOException{
SAXReader reader = new SAXReader();
Document document = reader.read("src/book2.xml");
Element root = document.getRootElement();
Element book2 =(Element)root.elements("书").get(1);
Element author2 = book2.element("作者");
author2.setText("wangm");
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book2.xml"),format);
writer.write(document);
writer.close();
}

}
0 0