一杯清茶

统计

留言簿

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("//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 一杯清茶 阅读(268) 评论(0)  编辑  收藏 所属分类: Hibernate