XML解析-Dom4j的DOM解析方式更新XML
生活随笔
收集整理的這篇文章主要介紹了
XML解析-Dom4j的DOM解析方式更新XML
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、更新XML
1.1、寫出內容到xml文檔
package com.rk.xml.g_dom4j_write;import java.io.File; import java.io.FileOutputStream;import org.dom4j.Document; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter;/*** 第一個寫出內容到xml文檔* @author RK**/ public class Demo01 {public static void main(String[] args) throws Exception{//一、讀取或創建一個Document對象Document doc = new SAXReader().read(new File("./src/animals.xml"));//二、修改Document對象內容//三、把修改后的Document對象寫出到xml文檔中//指定文件輸出的位置FileOutputStream outStream = new FileOutputStream("D:/rk/result.xml");//1.創建寫出對象XMLWriter writer = new XMLWriter(outStream);//2.寫出Document對象writer.write(doc);//3.關閉流writer.close();System.out.println("執行結束!");} }1.2、寫出內容到xml文檔的細節
package com.rk.xml.g_dom4j_write;import java.io.File; import java.io.FileOutputStream; import org.dom4j.Document; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter;/*** 討論寫出內容到xml文檔的細節* @author RK**/ public class Demo02 {public static void main(String[] args) throws Exception{Document doc = new SAXReader().read(new File("./src/animals2.xml"));/*** 1.指定寫出的格式*/ // OutputFormat format = OutputFormat.createCompactFormat();//緊湊的格式.去除空格換行.項目上線的時候OutputFormat format = OutputFormat.createPrettyPrint();//漂亮的格式.有空格和換行.開發調試的時候/*** 2.指定生成的xml文檔的編碼* 同時影響了xml文檔保存時的編碼 和 xml文檔聲明的encoding的編碼(xml解析時的編碼)* 結論: 使用該方法生成的xml文檔避免中文亂碼問題。*/format.setEncoding("UTF-8");//指定文件輸出的位置FileOutputStream outStream = new FileOutputStream("D:/rk/result.xml");//1.創建寫出對象XMLWriter writer = new XMLWriter(outStream, format);//2.寫出Document對象writer.write(doc);//3.關閉流writer.close();System.out.println("執行結束!");} }1.3、修改xml內容:添加、修改、刪除
package com.rk.xml.g_dom4j_write;import java.io.File; import java.io.FileOutputStream;import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Node; import org.dom4j.Element; import org.dom4j.Attribute; import org.dom4j.Text; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.dom4j.io.OutputFormat; import org.junit.Test;/*** 修改xml內容* 增加:文檔,標簽 ,屬性* 修改:屬性值,文本* 刪除:標簽,屬性* @author RK**/ public class Demo03 {/*** 增加:文檔,標簽 ,屬性*/@Testpublic void testAdd() throws Exception{//1.創建文檔Document doc = DocumentHelper.createDocument();//2.增加標簽Element rootElement = doc.addElement("ContactList");Element contactElement = rootElement.addElement("Contact");Element nameElement = contactElement.addElement("Name");nameElement.setText("小明");//3.增加屬性contactElement.addAttribute("id", "c001");contactElement.addAttribute("region", "北京");//把修改后的Document對象寫出到xml文檔中FileOutputStream out = new FileOutputStream("D:/rk/contact.xml");OutputFormat format = OutputFormat.createPrettyPrint();format.setEncoding("utf-8");XMLWriter writer = new XMLWriter(out,format);writer.write(doc);writer.close();System.out.println("執行結束!");}/*** 修改:屬性值,文本*/@Testpublic void testModify() throws Exception{Document doc = new SAXReader().read(new File("./src/animals.xml"));/*** 方案一: 修改屬性值 1.得到標簽對象 2.得到屬性對象 3.修改屬性值*///1.1 得到標簽對象Element catElement = doc.getRootElement().element("Cat");//1.2 得到屬性對象Attribute catAttr = catElement.attribute("id");//1.3 修改屬性值catAttr.setValue("c100");/*** 方案二: 修改屬性值*///2.1 得到標簽對象Element dogElement = doc.getRootElement().element("Dog");//2.2 通過增加同名屬性的方法,修改屬性值dogElement.addAttribute("id", "d100");/*** 修改文本 1.得到標簽對象 2.修改文本*/Element nameElement = doc.getRootElement().element("Cat").element("Home");nameElement.setText("第六宇宙");FileOutputStream out = new FileOutputStream("D:/rk/Animals.xml");OutputFormat format = OutputFormat.createPrettyPrint();format.setEncoding("utf-8");XMLWriter writer = new XMLWriter(out,format);writer.write(doc);writer.close();System.out.println("執行結束!");}/*** 刪除:標簽,屬性*/@Testpublic void testDelete() throws Exception{Document doc = new SAXReader().read(new File("./src/animals.xml"));/*** 1.刪除標簽 1.1 得到標簽對象 1.2 刪除標簽對象 */// 1.1 得到標簽對象Element nameElement = doc.getRootElement().element("Cat").element("Home");//1.2 刪除標簽對象nameElement.detach(); // nameElement.getParent().remove(nameElement);/*** 2.刪除屬性 2.1得到屬性對象 2.2 刪除屬性*/Element catElement = doc.getRootElement().element("Cat");//2.1得到屬性對象Attribute idAttr = catElement.attribute("id");//2.2 刪除屬性idAttr.detach(); // idAttr.getParent().remove(idAttr);FileOutputStream out = new FileOutputStream("D:/rk/animals.xml");OutputFormat format = OutputFormat.createPrettyPrint();format.setEncoding("utf-8");XMLWriter writer = new XMLWriter(out,format);writer.write(doc);writer.close();System.out.println("執行結束!");} }2、思維導圖
總結
以上是生活随笔為你收集整理的XML解析-Dom4j的DOM解析方式更新XML的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【小白学习PyTorch教程】十一、基于
- 下一篇: WeX5 3.8开发工具之蓝牙打印(全流