生活随笔
收集整理的這篇文章主要介紹了
jdom 插入 修改 删除
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
創(chuàng)建XML文檔
XML文件是一種典型的樹形文件,每個文檔元素都是一個document元素的子節(jié)點(diǎn)。而每個子元素都是一個Element對象,對象可以向下包含。
1 因此我們可以通過先創(chuàng)建元素再將元素添加到父元素中,最后將頂層元素添加到根元素中。
2?創(chuàng)建完文檔元素后,就可以把元素添加到document對象中,然后寫入文件。
主要使用的函數(shù):
Element.setAttribute 為元素添加信息Element.addContent(String,String) 為元素添加子元素內(nèi)容,也可以直接添加另一個元素節(jié)點(diǎn)Document.setRootElement(Element) 為文檔添加根元素XMLOutputter.output(Document,FileWriter) 將Docuemnt寫入到FileWriter文件流中
@SuppressWarnings("null") 2 ? ? public static void createXML() { 3 ? ? ? ? // 創(chuàng)建document 4 ? ? ? ? Document mydoc = new Document(); 5 6 ? ? ? ? // 創(chuàng)建元素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 ? ? ? ? // 創(chuàng)建元素person222 ? ? ? ? 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中添加元素Person37 ? ? ? ? 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 ? ? ? ? ? ? // 創(chuàng)建xml文件輸出流 5 ? ? ? ? ? ? XMLOutputter xmlopt = new XMLOutputter(); 6 7 ? ? ? ? ? ? // 創(chuàng)建文件輸出流 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 ? ? }?
執(zhí)行后,刷新項(xiàng)目,就可以在項(xiàng)目下看到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); ? ? }
總結(jié)
以上是生活随笔為你收集整理的jdom 插入 修改 删除的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。