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());
	}
}

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