自用文档,请不要转载!
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();
}
}