xml解析——增刪改查操作后將其修改結果保存


一、可擴展標記語言xml:Extensible Markup Language 
   1、XML的作用:1)統一數據傳輸的格式。2)做小型數據庫[文件型數據庫]。3)做配置文件  .ini   .propertity  .xml  .cfg 
   2、XML文件的基本格式: 
     標簽:分為雙標簽和單標簽,雙標簽的開頭和結尾標簽名必須一致,大小寫一樣,/ 開頭的是結尾標簽,單標簽必須在 > 前加上 / 來結尾,單標簽中不能放文本。 
     屬性:在開始標簽中定義一些名值對,值一定是字符串 
   3、XML文件的基本構成: 
     1)在第一行是XML聲明    <?xml version="1.0" encoding="UTF-8"  ?> 
     2)必須且只能有一對根標簽 
     3)標簽可以一層一層嵌 

二、寫一個簡單的xml文檔stus.xml然后用Dom進行解析。 
    DOM : Document Object Model 文檔對象模型 
    DOM解析的基本思路:將整個XML文件一次性讀入內存,將整個XML看做一棵文檔樹,XML中的每一個標簽,屬性,文本都看做是樹上的一個結點,然后可以對結點進行增刪改查的操作。 
   已經編輯好的stus.xml文檔 

Java代碼   收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <stus Class="1401" >  
  3.     <stu num="01">  
  4.         <name>張三</name>  
  5.         <age>19</age>  
  6.         <sex>男</sex>  
  7.     </stu>  
  8.     <stu  num="02">  
  9.         <name>李四</name>  
  10.         <age>20</age>  
  11.         <sex>女</sex>  
  12.     </stu>  
  13.     <stu  num="03">  
  14.         <name>王五</name>  
  15.         <age>21</age>  
  16.         <sex>男</sex>  
  17.     </stu>  
  18. </stus>  



三、開始解析 
    創建解析工廠 

Java代碼   收藏代碼
  1. // 得到解析工廠對象  
  2.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
  3.         // 生產一個解析器對象  
  4.         DocumentBuilder builder = factory.newDocumentBuilder();  
  5.         // 開始解析xml文件,得到的解析結果是一個Document對象,Document對象叫做文檔樹對象  
  6.         Document document = builder.parse("stus.xml");  


    1、增加節點 
    基本思路:首先創建一個新的元素節點,將元素節點追加到根節點后面,設置其節點屬性。創建標簽,設置標簽文本內容,最后將新標簽添加到新的元素節點中。 
代碼: 

Java代碼   收藏代碼
  1. // 創建一個新的元素節點  
  2.         Element stu = dom.createElement("stu");  
  3.         // 將元素節點追加到根節點后面  
  4.         root.appendChild(stu);  
  5.         // 設置節點屬性  
  6.         stu.setAttribute("num", "04");  
  7.         // 創建標簽  
  8.         Element name = dom.createElement("name");  
  9.         Element age = dom.createElement("age");  
  10.         Element sex = dom.createElement("sex");  
  11.         // 設置標簽文本內容  
  12.         name.setTextContent("趙六");  
  13.         age.setTextContent("19");  
  14.         sex.setTextContent("女");  
  15.         // 把標簽添加到新的元素節點stu中  
  16.         stu.appendChild(name);  
  17.         stu.appendChild(age);  
  18.         stu.appendChild(sex);  


    2、刪除節點 
基本思路:獲得要刪除的節點,然后得到節點的屬性值,與要刪除的節點的屬性值進行比較,如果該屬性值對應的節點存在則移除該節點。 
代碼: 

Java代碼   收藏代碼
  1. // 獲得根節點  
  2.         Element root = (Element) dom.getFirstChild();  
  3.         // 獲得所有stu節點  
  4.         NodeList list = dom.getElementsByTagName("stu");  
  5.         for (int i = 0; i < list.getLength(); i++) {  
  6.             Node node = list.item(i);  
  7.             if (node instanceof Element) {  
  8.                 Element e = (Element) node;  
  9.                 // 得到節點的屬性值,與要刪除的結點的屬性值進行比較,然后移除該屬性值對應的結點  
  10.                 String num = e.getAttribute("num");  
  11.                 if (num.equals("02")) {  
  12.                     root.removeChild(e);  
  13.                     break;  
  14.                 }  
  15.             }  
  16.         }  



    3、修改節點 
基本思路:獲得要修改的節點,修改其屬性值,然后獲得該節點下的標簽,修改標簽中的文本內容。 
代碼: 

Java代碼   收藏代碼
  1. // 修改節點屬性  
  2.         for (int j = 0; j < list.getLength(); j++) {  
  3.             Node no = list.item(j);  
  4.             if (no instanceof Element) {  
  5.                 Element el = (Element) no;  
  6.                 String n = el.getAttribute("num");  
  7.                 if (n.contains("01")) {  
  8.                     el.setAttribute("num", "05");  
  9.                     // 修改標簽值  
  10.                     NodeList li = el.getChildNodes();  
  11.                     for (int x = 0; x < li.getLength(); x++) {  
  12.                         Node d = li.item(x);  
  13.                         if (d instanceof Element) {  
  14.                             Element ee = (Element) d;  
  15.                             String noN = ee.getNodeName();  
  16.                             if (noN.equals("name")) {  
  17.                                 ee.setTextContent("小白");  
  18.                             } else if (noN.equals("age")) {  
  19.                                 ee.setTextContent("11");  
  20.                             } else if (noN.equals("sex")) {  
  21.                                 ee.setTextContent("男");  
  22.                             }  
  23.                         }  
  24.                     }  
  25.                 }  



4、查找節點 
基本思路:獲得所有的節點,用 需要查找的結點的屬性值與所有節點進行比較,如果該節點存在,就打印該節點的屬性值及其節點下標簽的內容。 
代碼: 

Java代碼   收藏代碼
  1. for (int j = 0; j < list.getLength(); j++) {  
  2.     Node no = list.item(j);  
  3.     if (no instanceof Element) {  
  4.         Element el = (Element) no;  
  5.         String n = el.getAttribute("num");  
  6.         //查找節點,顯示其屬性值及標簽內容  
  7.         if(n.equals("03")){  
  8.             System.out.println(no.getNodeName()+"\t"+n+no.getTextContent());  
  9.         }  
  10.     }  
  11. }  




4、保存修改后的xml文檔 
基本思路:先將內存中的Document對象寫到xml文件中,然后將整個Document對象作為要寫入xml文件的數據源,最后將數據源寫入目標文件。 
代碼: 

Java代碼   收藏代碼
  1. // 將內存中的Document對象寫到xml文件中  
  2.         TransformerFactory tf = TransformerFactory.newInstance();  
  3.         Transformer former = tf.newTransformer();  
  4.         former.setParameter("version", "1.0");  
  5.         former.setParameter("encoding", "GBK");  
  6.         // 將整個Document對象作為要寫入xml文件的數據源  
  7.         DOMSource xmlSource = new DOMSource(dom);  
  8.         // 要寫入的目標文件  
  9.         StreamResult outputTarget = new   
  10.                 StreamResult(new File("F:\\stus2.xml"));  
  11.         former.transform(xmlSource, outputTarget);  

 

  http://www.99inf.com/jinrong/yhbx/1071334.html

  http://www.99inf.com/jinrong/yhbx/1071318.html

  http://www.99inf.com/jinrong/yhbx/1071308.html

  http://www.99inf.com/jinrong/yhbx/1071302.html

  http://www.99inf.com/jinrong/yhbx/1071288.html

  http://www.xianhuo8.cn/bbs/thread-156595-1-1.html

  http://www.xianhuo8.cn/bbs/thread-156610-1-1.html

  http://www.diyizby.com/bbs/thread-4275199-1-1.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM