Dom4j常用操作备忘!

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.LinkedList;
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 {
	/**
	 * 方法创建一个XML文件,并设置文件节点。
	 * 
	 * @param 无参数
	 * @return void 无返回
	 * @exception try IOException
	 */
	public static void dom4jCreateXml() {
		Document doc = DocumentHelper.createDocument();
		doc.addComment("这是一个保存学生信息的XML文档");
		Element root = doc.addElement("students");
		Element stuEle = root.addElement("student");
		stuEle.addAttribute("id", "1");
		Element nameEle = stuEle.addElement("name");
		nameEle.setText("zhangsan");
		Element sexEle = stuEle.addElement("sex");
		sexEle.setText("man");
		Element ageEle = stuEle.addElement("age");
		ageEle.setText("20");
		Writer out;
		try {
			out = new FileWriter("src/student.xml");
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("GBK");
			XMLWriter writer = new XMLWriter(out, format);
			writer.write(doc);
			writer.close();
			System.out.print("生成XML文件成功");
		} catch (IOException e) {
			System.out.print("生成XML文件失败5ee4463499dUadhh");
			e.printStackTrace();
		}
	}

	/**
	 * 方法用于为XML添加一个节点
	 * 
	 * @param 无参数
	 * @return void 无返回类型
	 * @exception throws DocumentException, IOException
	 */
	public static void addElement() throws DocumentException, IOException {
		File file = new File("src/student.xml");
		SAXReader reader = new SAXReader();
		Document doc = reader.read(file);
		Element root = doc.getRootElement();
		Element stuEle = root.addElement("student");
		stuEle.addAttribute("id", "2");
		Element nameEle = stuEle.addElement("name");
		nameEle.setText("lisi");
		Element sexEle = stuEle.addElement("sex");
		sexEle.setText("nv");
		Element ageEle = stuEle.addElement("age");
		ageEle.setText("21");
		XMLWriter writer = new XMLWriter(new FileWriter(file));
		writer.write(doc);
		writer.close();
	}

	/**
	 * 方法用于修改某个节点的属性或值
	 * 
	 * @param 无参数
	 * @return void 无返回
	 * @exception throws DocumentException, IOException
	 * */
	@SuppressWarnings("unchecked")
	public static void modifyElement() throws DocumentException, IOException {
		LinkedList l;
		File file = new File("src/student.xml");
		SAXReader reader = new SAXReader();
		Document doc = reader.read(file);
		Element root = doc.getRootElement();
		List<Element> student = root.elements("student");
		for (int i = 0; i < student.size(); i++) {
			if (student.get(i).attributeValue("id").equals("2")) {
				student.get(i).element("name").setText("wangwu");
				XMLWriter writer = new XMLWriter(new FileWriter(file));
				writer.write(doc);
				writer.close();
				break;
			}
		}
	}

	/**
	 * 方法用于删除文件中的某个节点
	 * 
	 * @param 无参数
	 * @return void 无返回
	 * @exception throws DocumentException, IOException
	 * */
	@SuppressWarnings("unchecked")
	public static void deleteElement() throws DocumentException, IOException {
		File file = new File("src/student.xml");
		SAXReader reader = new SAXReader();
		Document doc = reader.read(file);
		Element root = doc.getRootElement();
		List<Element> student = root.elements("student");
		for (Element ele : student) {
			if (ele.attributeValue("id").equals("2")) {
				root.remove(ele);
				XMLWriter writer = new XMLWriter(new FileWriter(file));
				writer.write(doc);
				writer.close();
				break;
			}
		}
	}

	/**
	 * 方法用于读取整个XML文件,显示各个节点的值
	 * 
	 * @param 无参数
	 * @return void 无返回类型
	 * @exception throws DocumentException
	 * */
	@SuppressWarnings( { "unused", "unchecked" })
	private static void dom4jReadXml() throws DocumentException {
		File file = new File("src/em.xml");
		SAXReader reader = new SAXReader();
		Document doc = reader.read(file);
		
		Element root = doc.getRootElement();
		List<Element> list = root.elements("student");
		for (Element ele : list) {
			String name = ele.elementText("name");
			String age = ele.elementText("age");
			String sex = ele.elementText("sex");
			System.out.println(name + " " + age + " " + sex);
		}
	}
	
	public static void main(String[] args) throws DocumentException {
		dom4jReadXml();
	}
}

 

你可能感兴趣的:(xml,dom4j)