甜咖啡

          我的IT空間

          JDOM生成和解析XML

          4.JDOM生成和解析XML  

          為減少DOM、SAX的編碼量,出現了JDOM;優點:20-80原則,極大減少了代碼量。使用場合:要實現的功能簡單,如解析、創建等,但在底層,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)  編輯  收藏


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


          網站導航:
           

          導航

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

          統計

          常用鏈接

          留言簿(1)

          我參與的團隊

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 綦江县| 得荣县| 岚皋县| 普兰店市| 丽水市| 聂拉木县| 监利县| 伊吾县| 汪清县| 依兰县| 大石桥市| 横山县| 银川市| 剑川县| 灵川县| 汤阴县| 吉林市| 广灵县| 新和县| 随州市| 乐平市| 镶黄旗| 三穗县| 云阳县| 太湖县| 永年县| 吴旗县| 沂南县| 玛沁县| 仙游县| 巧家县| 清水河县| 来宾市| 定西市| 通州市| 凭祥市| 日土县| 同德县| 和田市| 东乡族自治县| 泾川县|