XML 读取 写入 更新 Dom4J 操作

package cn.com.agree.fly.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.dom4j.Attribute;
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;
/**
 * XML基本读取写入(dom4j操作)
 * @author 李海云
 * @mail [email protected]
 * @date 2012-01-11 14:25
 */
public class XMLReadWriter_Dom4J {
	public static void main(String[] args) throws DocumentException, IOException {
		writeXML("d:/product.xml");
		readXML("d:/product.xml");
		System.out.println("-----------");
		modifyXML("d:/product.xml");
		readXML("d:/product.xml");
	}
	public static void writeXML(String filePath) throws IOException {
		Document document = DocumentHelper.createDocument();//创建xml文档对象
		Element root = DocumentHelper.createElement("product");//创建根节点
		document.setRootElement(root);//传入根目录
		root.addAttribute("name","oracle");//添加根节点的属性
		Element priceElement = root.addElement("price");//添加price标签元素
		priceElement.setText("22.5");//设置元素值
		Element descElement = root.addElement("desc");
		descElement.setText("我是产品描述啊!");
		descElement.addAttribute("title", "我是描述标题啊");//给元素设置属性
		OutputFormat format = new OutputFormat("    ",true);//创建格式化模版对象
		XMLWriter xmlWriter2 = new XMLWriter(new FileOutputStream(filePath),format);//创建xml写入器
		xmlWriter2.write(document);//执行写入
		xmlWriter2.flush();
		xmlWriter2.close();
	}
	public static void readXML(String filePath) throws DocumentException, IOException {
		File file = new File(filePath);
		SAXReader saxReader = new SAXReader();//创建读取器
		Document document = saxReader.read(file);//读取xml到document对象中
		Element rootElement = document.getRootElement();//获得文档根目录
		System.out.println(rootElement.getName());//读取根目录名称
		System.out.println(rootElement.attributeValue("name"));//读取更目录属性
		Element priceElement = rootElement.element("price");//获得标签为price的节点
		System.out.println(priceElement.getText());//获取其值
	}
	private static void modifyXML(String filePath) throws IOException, DocumentException {
		File file = new File(filePath);
		SAXReader saxReader = new SAXReader();//创建读取器
		Document document = saxReader.read(file);//读取xml到document对象中
		Element rootElement = document.getRootElement();//获得文档根目录
		rootElement.setName("user");
		Attribute nameAttribute = rootElement.attribute("name");
		nameAttribute.setText("haha");
		Element priceElement = rootElement.element("price");//获得标签为price的节点
		priceElement.setText("88.8");//修改值
		Element descElement = rootElement.element("desc");
		descElement.setText("我是描述2222222");
		XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(filePath));//创建写入器
		xmlWriter.write(document);//写入最新document对象到文件中
		xmlWriter.flush();
		xmlWriter.close();
//		//查看是否更新成功
//		System.out.println(new SAXReader().read(new File(filePath)).getRootElement().element("price").getText());
	}
}

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章