一杯清茶

          統計

          留言簿

          Oracle SQL/PLSQL

          PowerDesigner教程系列

          Struts2.0

          web開發

          三人行

          從事RCP開發的同行

          工作流和權限設置

          閱讀排行榜

          評論排行榜

          使用dom4j讀取xml文檔,并修改文檔

          自用文檔,請不要轉載!


          public void prot() throws Exception{   
                      try {
                          String path=FileLocator.toFileURL(Platform.getBundle(AppConn.APPLICATION_ID).getEntry("")).getPath()
                                                 .toString()+"src/hibernate.cfg.xml";
                          File cfgFile=new File(path);
                          SAXReader reader=new SAXReader();           
                          reader.setEntityResolver(new EntityResolver()      
                          {      
                              public InputSource resolveEntity(String publicId,      
                                      String systemId)      
                              {      
                                  if (publicId.equals("-//Hibernate/Hibernate Configuration DTD 3.0//EN"))      
                                  {      
                                      InputStream in = getClass().getResourceAsStream(      
                                              "/hibernate-configuration-3.0.dtd");      
                                      return new InputSource(in);      
                                  }      
                                  return null;      
                              }      
                          });            
                          Document document=null;
                          try {
                              document = reader.read(cfgFile);
                          } catch (DocumentException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                          }
                          Element root=document.getRootElement();
                          List list=root.selectNodes("http://property");
                          Iterator it=list.iterator();
                          while(it.hasNext()){
                              Element nextelement=(Element)it.next();
                              String  text=nextelement.attributeValue("name");
                              if(text.equals("connection.username")){
                                      nextelement.setText("");                                                               
                              }
                              if(text.equals("connection.password")){                       
                                      nextelement.setText("");                   
                              }
                              if(text.equals("connection.url")){
                                      nextelement.setText("");                                       
                              }               
                          }               
                              XMLWriter output=new XMLWriter(new FileWriter(cfgFile),OutputFormat.createPrettyPrint());
                              output.write(document);
                              output.flush();
                              output.close();
                     
                      } catch (HibernateException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }    
                     
                  }

          posted on 2008-04-23 15:06 一杯清茶 閱讀(287) 評論(0)  編輯  收藏 所屬分類: Hibernate

          主站蜘蛛池模板: 新建县| 绥阳县| 靖远县| 丽江市| 庐江县| 临沭县| 翼城县| 屏东市| 金坛市| 沁阳市| 镇原县| 托里县| 嘉兴市| 离岛区| 酒泉市| 抚宁县| 新昌县| 长垣县| 绩溪县| 白山市| 新巴尔虎右旗| 龙州县| 利川市| 嵊州市| 南溪县| 会东县| 玉山县| 体育| 宜川县| 团风县| 买车| 兴安县| 大田县| 庄浪县| 土默特左旗| 宕昌县| 静安区| 武汉市| 营山县| 固原市| 叶城县|