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 CloudComputing.cc@gmail.com
 * @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());
	}
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值