Dom4j工具--XML的DOM解析(下)--写操作
生活随笔
收集整理的這篇文章主要介紹了
Dom4j工具--XML的DOM解析(下)--写操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言:
上一篇博客我開始了用Dom4j對XML進行了讀操作,這篇博客主要是進行對XML的寫操作 只涉及基礎的內容,只要有javase基礎和eclipse的使用 就可以完成, 往后的內容包括 “框架” 我都還沒學到,所以本文中的內容也都是局限于基礎部分有興趣的可以參考Dom4j工具--XML的DOM解析(上)--讀操作
也可以參考DOM4J官網
還可以查看DOM4J API
目錄:
1. 如何寫內容到XML 2. 增:文檔,標簽,屬性,文本內容 3. 改:屬性值,文本 4. 刪:標簽,屬性現在開始正文。
如何寫內容到XML:
這一步是所有操作的前提,也是入門的必要操作
簡單舉個栗子:
實現對一個xml文檔的粘貼復制功能
結果:
createCompactFormat():
createPrettyPrint():
<?xml version="1.0" encoding="UTF-8"?><constant> <cons> <name>hello1</name> </cons> <name>hello2</name> <cons> <name>hello3</name> </cons> </constant>注意:
單元測試--JUnit4了解一下(eclipse環境)
outputFormat2.setEncoding("UTF-8");是指定保存的編碼格式為UTF-8
修改:
outputFormat2.setEncoding("GBK");內容是中文的話 就會出現亂碼
outputFormat2.setEncoding會使得 保存的編碼格式和文檔聲明一致 即
增:文檔,標簽,屬性,文本內容:
DocumentHelper.createDocument() 增加文檔
addElement("名稱") 增加標簽
addAttribute("名稱",“值”) 增加屬性
addText(“內容”) 增加文本內容
源代碼:
@Testpublic void test2() throws Exception {FileOutputStream fileOutputStream=new FileOutputStream("D:\\xx.xml");Document doc=DocumentHelper.createDocument();//創建文檔Element element=doc.addElement("age1"); //創建標簽element.addAttribute("id", "12"); //創建屬性element.addText("Text"); //創建文本文件OutputFormat outputFormat2=OutputFormat.createPrettyPrint();outputFormat2.setEncoding("UTF-8");XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);xmlWriter.write(doc);xmlWriter.close();}注意:
Element element=doc.addElement("age1");不能重復插入,因為只有一個根標簽
修改:屬性值,文本:
Attribute.setValue("值") 修改屬性值 Element.addAtribute("同名的屬性名","值") 修改同名的屬性值 Element.setText("內容") 修改文本內容 @Testpublic void test2() throws Exception {// 1.讀取xml文檔,返回Document對象SAXReader reader = new SAXReader();Document doc = reader.read(new File(".\\src\\day33\\ss.xml"));FileOutputStream fileOutputStream=new FileOutputStream("D:\\xx.xml");//獲取標簽對象Element element=doc.getRootElement();Element elementSon=element.element("cons");//修改屬性值elementSon.addAttribute("id", "12"); // 通過增加同名屬性的方法,修改屬性值Attribute attribute=elementSon.attribute("id"); // 獲取屬性對象 修改屬性值值attribute.setValue("13");;elementSon.setText("修改1"); //修改文本內容OutputFormat outputFormat2=OutputFormat.createPrettyPrint();outputFormat2.setEncoding("UTF-8");XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);xmlWriter.write(doc);xmlWriter.close();}注意:cons標簽下先文本后name標簽,如果修改cons文本內容后 ,文本會下移到name標簽下。似乎沒啥大影響
刪:標簽,屬性:
Element.detach(); 刪除標簽 Attribute.detach(); 刪除屬性源代碼:
@Testpublic void test2() throws Exception {// 1.讀取xml文檔,返回Document對象SAXReader reader = new SAXReader();Document doc = reader.read(new File(".\\src\\day33\\ss.xml"));FileOutputStream fileOutputStream=new FileOutputStream("D:\\xx.xml");//獲取標簽對象Element element=doc.getRootElement();Element elementSon=element.element("cons");//獲取到cons下第一個標簽節點并且刪除elementSon.elements().get(0).detach();//獲取到cons標簽的屬性為id的屬性對象,然后刪除Attribute idAttribute=elementSon.attribute("id");idAttribute.detach();OutputFormat outputFormat2=OutputFormat.createPrettyPrint();outputFormat2.setEncoding("UTF-8");XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);xmlWriter.write(doc);xmlWriter.close();}總結
以上是生活随笔為你收集整理的Dom4j工具--XML的DOM解析(下)--写操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2015第14周五
- 下一篇: Backbone React Requi