甜咖啡

          我的IT空間

          JDOM生成和解析XML

          4.JDOM生成和解析XML  

          為減少DOM、SAX的編碼量,出現(xiàn)了JDOM;優(yōu)點:20-80原則,極大減少了代碼量。使用場合:要實現(xiàn)的功能簡單,如解析、創(chuàng)建等,但在底層,JDOM還是使用SAX(最常用)、DOM、Xanan文檔。

          import java.io.FileNotFoundException;  
          import java.io.FileOutputStream;  
          import java.io.IOException;  
          import java.util.List;  

          import org.jdom.Document;  
          import org.jdom.Element;  
          import org.jdom.JDOMException;  
          import org.jdom.input.SAXBuilder;  
          import org.jdom.output.XMLOutputter;  
          /** 
          *  
          * @author hongliang.dinghl 
          * JDOM 生成與解析XML文檔 
          *  
          */ 
          public class JDomDemo implements XmlDocument {  

          public void createXml(String fileName) {  
          Document document;  
          Element  root;  
          root=new Element("employees");  
          document=new Document(root);  
          Element employee=new Element("employee");  
          root.addContent(employee);  
          Element name=new Element("name");  
          name.setText("ddvip");  
          employee.addContent(name);  
          Element sex=new Element("sex");  
          sex.setText("m");  
          employee.addContent(sex);  
          Element age=new Element("age");  
          age.setText("23");  
          employee.addContent(age);  
          XMLOutputter XMLOut = new XMLOutputter();  
          try {  
          XMLOut.output(document, new FileOutputStream(fileName));  
          } catch (FileNotFoundException e) {  
          e.printStackTrace();  
          } catch (IOException e) {  
          e.printStackTrace();  
          }  

          }  

          public void parserXml(String fileName) {  
          SAXBuilder builder=new SAXBuilder(false);   
          try {  
          Document document=builder.build(fileName);  
          Element employees=document.getRootElement();   
          List employeeList=employees.getChildren("employee");  
          for(int i=0;iElement employee=(Element)employeeList.get(i);  
          List employeeInfo=employee.getChildren();  
          for(int j=0;jSystem.out.println(((Element)employeeInfo.get(j)).getName()+":"+((Element)employeeInfo.get(j)).getValue());  

          }  
          }  
          } catch (JDOMException e) {  

          e.printStackTrace();  
          } catch (IOException e) {  

          e.printStackTrace();  
          }   

          }  
          }

          posted on 2011-07-19 16:33 甜咖啡 閱讀(194) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           

          導航

          <2011年7月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統(tǒng)計

          常用鏈接

          留言簿(1)

          我參與的團隊

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 肃北| 济阳县| 潼关县| 宜章县| 弥渡县| 东阳市| 华坪县| 高青县| 白河县| 新竹县| 松滋市| 阿拉尔市| 浦城县| 密山市| 荣成市| 洛阳市| 江口县| 邻水| 新兴县| 仁寿县| 湖南省| 报价| 元谋县| 蓬溪县| 秭归县| 通州市| 安宁市| 贵阳市| 集贤县| 张家口市| 霍城县| 布拖县| 天气| 南乐县| 南华县| 方山县| 沅江市| 安康市| 紫云| 开原市| 仪陇县|