日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

Dom4j工具--XML的DOM解析(下)--写操作

發布時間:2025/3/17 asp.net 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Dom4j工具--XML的DOM解析(下)--写操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:

上一篇博客我開始了用Dom4j對XML進行了讀操作,這篇博客主要是進行對XML的寫操作 只涉及基礎的內容,只要有javase基礎和eclipse的使用 就可以完成, 往后的內容包括 “框架” 我都還沒學到,所以本文中的內容也都是局限于基礎部分

有興趣的可以參考Dom4j工具--XML的DOM解析(上)--讀操作
也可以參考DOM4J官網
還可以查看DOM4J API

目錄:

1. 如何寫內容到XML 2. 增:文檔,標簽,屬性,文本內容 3. 改:屬性值,文本 4. 刪:標簽,屬性

現在開始正文。

如何寫內容到XML:

這一步是所有操作的前提,也是入門的必要操作

簡單舉個栗子:
實現對一個xml文檔的粘貼復制功能

@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");OutputFormat outputFormat1=OutputFormat.createCompactFormat();OutputFormat outputFormat2=OutputFormat.createPrettyPrint();outputFormat2.setEncoding("UTF-8");XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);xmlWriter.write(doc);xmlWriter.close();}

結果:
createCompactFormat():

<?xml version="1.0" encoding="UTF-8"?> <constant><cons><name>hello1</name></cons><name>hello2</name><cons><name>hello3</name></cons></constant>

createPrettyPrint():

<?xml version="1.0" encoding="UTF-8"?><constant> <cons> <name>hello1</name> </cons> <name>hello2</name> <cons> <name>hello3</name> </cons> </constant>

注意:

  • @Test可以忽略 因為涉及到注解和單元測試,想了解單元測試的可以參考一下這篇:
    單元測試--JUnit4了解一下(eclipse環境)
  • XMLWriter導入的時候注意頭文件別錯了 是:import org.dom4j.io.XMLWriter;
  • 這里使用FileOutputStream 而不是Writer字符流 因為防止考慮編碼
  • 記得最后要關閉流。
  • createCompactFormat():緊湊的結構 去除空格和換行,項目上線 因為xml更小
  • createPrettyPrint(): 漂亮的結構 有空格和換行,開發調試
    outputFormat2.setEncoding("UTF-8");是指定保存的編碼格式為UTF-8
  • 修改:
    outputFormat2.setEncoding("GBK");內容是中文的話 就會出現亂碼
    outputFormat2.setEncoding會使得 保存的編碼格式和文檔聲明一致 即

    <?xml version="1.0" encoding="GBK"?>

    增:文檔,標簽,屬性,文本內容:

    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解析(下)--写操作的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。