dom4j解析、修改、生成xml文件

項目中用到XML的地方非常普遍,XML也不是一種新的技術。但經常遇到朋友們問如何操作XML文件。所以寫一個小例子,代碼很亂。寫出來和大家一起交流學習 

1、生成XML 
Java代碼  收藏代碼
  1. public void createApplicationConfigXML(){  
  2.          //建立document對象  
  3.          try {  
  4.                  Document document = DocumentHelper.createDocument();  
  5.                  Element root = document.addElement("root");//添加文檔根  
  6.                  root.addComment("這個一個註釋");//加入一行註釋  
  7.                  Element request = root.addElement("request"); //添加root的子節點  
  8.                  request.addAttribute("type""cat");  
  9.                  request.addAttribute("flow""tong");  
  10.                  request.addAttribute("time""2009");  
  11.                  Element pro = request.addElement("pro");  
  12.                  pro.addAttribute("type""att");  
  13.                  pro.addAttribute("name""附件");  
  14.                  pro.addText("測試哈子");  
  15.                    
  16.                  Element cd = request.addElement("pro");  
  17.                  cd.addAttribute("type""cd");  
  18.                  cd.addAttribute("name""特殊字符過濾");  
  19.                  cd.addCDATA("特殊字符");  
  20.                    
  21.                  //輸出全部原始數據,在編譯器中顯示  
  22.                  OutputFormat format = OutputFormat.createPrettyPrint();  
  23.                  format.setEncoding("GBK");//根據需要設置編碼  
  24.                  XMLWriter writer = new XMLWriter(System.out, format);  
  25.                  document.normalize();  
  26.                  writer.write(document);    
  27.                  writer.close();  
  28.                  // 輸出全部原始數據,並用它生成新的我們需要的XML文件  
  29.                  XMLWriter writer2 = new XMLWriter(new FileWriter(new File(  
  30.                  "test.xml")), format);  
  31.                  writer2.write(document); //輸出到文件  
  32.                  writer2.close();  
  33.         } catch (UnsupportedEncodingException e) {  
  34.             // TODO Auto-generated catch block  
  35.             e.printStackTrace();  
  36.         } catch (IOException e) {  
  37.             // TODO Auto-generated catch block  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  

----------------------------------------------------------------- 
下面是解析和修改XML 
XML文件內容如下: 
Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <root>  
  3.     <request type="Pending" flowType="GENERAL" flowName="報銷流程"  
  4.         docId="185647" flowId="16409" nodeName="報銷人確認" wikId="58288"  
  5.         sendId="1210040" userId="1210040" createDate="2009-12-03"  
  6.         title="費用報銷 " flowCreaterId="1210040" nodeType="1"  
  7.         bosTime="2009-12-03 09:36:15">  
  8.         <pro type="att"></pro>  
  9.         <pro type="textarea" name="OP_bxryj" title="處理意見" need="true"></pro>  
  10.     </request>  
  11. </root>  

修改 
Java代碼  收藏代碼
  1. public String getApplcationConfigFromXMLTest(){  
  2.     String value = "";  
  3.     try {  
  4.         SAXReader sax = new SAXReader();  
  5.         Document xmlDoc = sax.read(new File(this.UBSSDIC_PATH));  
  6.         Element root = xmlDoc.getRootElement();//根節點  
  7.         Iterator it = root.elementIterator();  
  8.         while(it.hasNext()){  
  9.             Element ele = (Element)it.next();  
  10.             Attribute attribute = ele.attribute("type");  
  11.             if(attribute.getStringValue().equals("Pending")){  
  12.                 attribute.setValue("sendread2");//修改屬性節點的值  
  13.             }  
  14.   
  15.             Attribute flowType = ele.attribute("flowType");  
  16.             flowType.detach();//刪除某個屬性  
  17.               
  18.             ele.addAttribute("type""Pending");//添加一個屬性節點  
  19.         }  
  20.         Element new_cdata = root.addElement("new_cdata");//添加一個元素  
  21.         new_cdata.addCDATA("tst&ree");  
  22.           
  23.         Element new_ele = root.addElement("new_ele");//添加一個元素  
  24.         new_ele.addText("33434343");  
  25.   
  26.         Element obj = (Element)root.selectObject("//pro[@type='att']");//根據XPath查找元素  
  27.         obj.setText("測試dddddd");//修改元素的值 即text節點  
  28.              //輸出全部原始數據,在編譯器中顯示  
  29.            OutputFormat format = OutputFormat.createPrettyPrint();  
  30.            format.setEncoding("GBK");  
  31.            XMLWriter writer = new XMLWriter(System.out, format);  
  32.            writer.write(xmlDoc);    
  33.            writer.close();  
  34.            // 輸出全部原始數據,並用它生成新的我們需要的XML文件  
  35.            XMLWriter writer2 = new XMLWriter(new FileWriter(new File(  
  36.              "test.xml")), format);  
  37.            writer2.write(xmlDoc); //輸出到文件  
  38.            writer2.close();  
  39.     } catch (DocumentException e) {  
  40.         System.out.println(e.getMessage());  
  41.         e.printStackTrace();  
  42.     }catch(IOException e){  
  43.         e.printStackTrace();  
  44.     }  
  45.     return value ;  
  46. }  

解析 
Java代碼  收藏代碼
  1. public void parseApplicationConfigXML(){  
  2.     try {  
  3.         SAXReader sax = new SAXReader();  
  4.         Document xmlDoc = sax.read(new File("E:\\20090316HPS\\Dom4jTest\\t.xml"));  
  5.         Element root = xmlDoc.getRootElement();//根節點  
  6.         Iterator it = root.elementIterator("request");  
  7.         while(it.hasNext()){  
  8.             Element request = (Element)it.next();  
  9.             System.out.println(request.getName());  
  10.             System.out.println(request.attributeValue("type"));  
  11.             System.out.println(request.attributeValue("flow"));  
  12.             System.out.println(request.attributeValue("time"));  
  13.               
  14.         }  
  15.         System.out.println("-------------------------------------------");  
  16.         List list = root.selectNodes("//pro");  
  17.         for(int i = 0; i < list.size(); i++){  
  18.             Element pro = (Element)list.get(i);  
  19.             System.out.println(pro.getName());  
  20.             System.out.println(pro.attributeValue("type"));  
  21.             System.out.println(pro.attributeValue("name"));  
  22.             System.out.println(pro.getText());  
  23.             System.out.println("+++++++++++++++++++++++++++++++++");  
  24.         }  
  25.     } catch (DocumentException e) {  
  26.         // TODO Auto-generated catch block  
  27.         e.printStackTrace();  
  28.     }  
  29. }  
發佈了29 篇原創文章 · 獲贊 14 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章