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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jdom 插入 修改 删除

發布時間:2025/7/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jdom 插入 修改 删除 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

創建XML文檔

  XML文件是一種典型的樹形文件,每個文檔元素都是一個document元素的子節點。而每個子元素都是一個Element對象,對象可以向下包含。

  1 因此我們可以通過先創建元素再將元素添加到父元素中,最后將頂層元素添加到根元素中。

  2?創建完文檔元素后,就可以把元素添加到document對象中,然后寫入文件。

  主要使用的函數:

Element.setAttribute 為元素添加信息Element.addContent(String,String) 為元素添加子元素內容,也可以直接添加另一個元素節點Document.setRootElement(Element) 為文檔添加根元素XMLOutputter.output(Document,FileWriter) 將Docuemnt寫入到FileWriter文件流中


  • @SuppressWarnings("null")
  • 2 ? ? public static void createXML() {
  • 3 ? ? ? ? // 創建document
  • 4 ? ? ? ? Document mydoc = new Document();
  • 5
  • 6 ? ? ? ? // 創建元素person1
  • 7 ? ? ? ? Element person1 = new Element("person");
  • 8 ? ? ? ? person1.setAttribute("id", "ID001");
  • 9 ? ? ? ? // 添加注釋
  • 10 ? ? ? ? person1.addContent(new Comment("this is person1"));
  • 11
  • 12 ? ? ? ? person1.addContent(new Element("name").setText("xingoo"));
  • 13 ? ? ? ? person1.addContent(new Element("age").setText("25"));
  • 14 ? ? ? ? person1.addContent(new Element("sex").setText("M"));
  • 15 ? ? ? ? // 可以嵌套添加子元素
  • 16 ? ? ? ? Element address1 = new Element("address");
  • 17 ? ? ? ? address1.setAttribute("zone", "province");
  • 18 ? ? ? ? address1.addContent("LiaoNing");
  • 19 ? ? ? ? person1.addContent(address1);
  • 20
  • 21 ? ? ? ? // 創建元素person2
  • 22 ? ? ? ? Element person2 = new Element("person");
  • 23 ? ? ? ? person2.setAttribute("id", "ID002");
  • 24 ? ? ? ? // 添加注釋
  • 25 ? ? ? ? person2.addContent(new Comment("this is person2"));
  • 26
  • 27 ? ? ? ? person2.addContent(new Element("name").setText("xhalo"));
  • 28 ? ? ? ? person2.addContent(new Element("age").setText("26"));
  • 29 ? ? ? ? person2.addContent(new Element("sex").setText("M"));
  • 30 ? ? ? ? // 可以嵌套添加子元素
  • 31 ? ? ? ? Element address2 = new Element("address");
  • 32 ? ? ? ? address2.setAttribute("zone", "province");
  • 33 ? ? ? ? address2.addContent("JiLin");
  • 34 ? ? ? ? person2.addContent(address2);
  • 35
  • 36 ? ? ? ? // 在doc中添加元素Person
  • 37 ? ? ? ? Element info = new Element("information");
  • 38 ? ? ? ? info.addContent(person1);
  • 39 ? ? ? ? info.addContent(person2);
  • 40 ? ? ? ? mydoc.setRootElement(info);
  • 41 ? ? ? ?
  • 42 ? ? ? ? saveXML(mydoc);
  • 43 ? ? }

  • saveXML()代碼:

  • 1 ? ? public static void saveXML(Document doc) {
  • 2 ? ? ? ? // 將doc對象輸出到文件
  • 3 ? ? ? ? try {
  • 4 ? ? ? ? ? ? // 創建xml文件輸出流
  • 5 ? ? ? ? ? ? XMLOutputter xmlopt = new XMLOutputter();
  • 6
  • 7 ? ? ? ? ? ? // 創建文件輸出流
  • 8 ? ? ? ? ? ? FileWriter writer = new FileWriter("person.xml");
  • 9
  • 10 ? ? ? ? ? ? // 指定文檔格式
  • 11 ? ? ? ? ? ? Format fm = Format.getPrettyFormat();
  • 12 ? ? ? ? ? ? // fm.setEncoding("GB2312");
  • 13 ? ? ? ? ? ? xmlopt.setFormat(fm);
  • 14
  • 15 ? ? ? ? ? ? // 將doc寫入到指定的文件中
  • 16 ? ? ? ? ? ? xmlopt.output(doc, writer);
  • 17 ? ? ? ? ? ? writer.close();
  • 18 ? ? ? ? } catch (Exception e) {
  • 19 ? ? ? ? ? ? e.printStackTrace();
  • 20 ? ? ? ? }
  • 21 ? ? }

  • ?



     執行后,刷新項目,就可以在項目下看到person.xml文件了


    修改同理


    刪除 :


  • ? ? public static void removeXML() {
  • ? ? ? ? SAXBuilder sb = new SAXBuilder();
  • ? ? ? ? Document doc = null;
  • ? ? ? ? try {
  • ? ? ? ? ? ? doc = sb.build("person.xml");
  • ? ? ? ? ? ? Element root = doc.getRootElement();
  • ? ? ? ? ? ? List<Element> list = root.getChildren("person");
  • ? ? ? ? ? ? for (Element el : list) {
  • ? ? ? ? ? ? ? ? if (el.getAttributeValue("id").equals("ID001")) {
  • ? ? ? ? ? ? ? ? ? ? root.removeContent(el);
  • ? ? ? ? ? ? ? ? }
  • ? ? ? ? ? ? }
  • ? ? ? ? } catch (Exception e) {
  • ? ? ? ? ? ? e.printStackTrace();
  • ? ? ? ? }
  • ? ? ? ? saveXML(doc);
  • ? ? }




  • 總結

    以上是生活随笔為你收集整理的jdom 插入 修改 删除的全部內容,希望文章能夠幫你解決所遇到的問題。

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