|
发表于 2020-6-8 09:45:01
|
显示全部楼层
/**
* 读取xml文件
*
* @param realPath :
* web路径
*/
public Document procConfig(String realPath) {
realPath += cfgFile;
this.file = new File(realPath);
try {
SAXBuilder builder = new SAXBuilder();
doc = builder.build(new File(realPath));
return doc;
} catch (Exception e) {
System.err.println("Error creating XML parser in UsrConfig.java");
e.printStackTrace();
return null;
}
}
/**
* 读取usrcfg.xml中的元素:主机的IP地址,及端口
*
* @param doc
*/
public void setUsrInfo(Document doc, String addr, String port,
String usrName, String usrPwd) {
Element root = doc.getRootElement();
Element host = root.getChild(UsrDef.TAG_HOST);
host.setAttribute(UsrDef.ATTR_ADDR, addr);
host.setAttribute(UsrDef.ATTR_PORT, port);
Element user = root.getChild(UsrDef.TAG_USER);
user.setAttribute(UsrDef.ATTR_NAME, usrName);
user.setAttribute(UsrDef.ATTR_PWD, usrPwd);
this.saveProperties();
}/**
* 读取usrcfg.xml中的元素:用户名,密码,数据库的数据源,数据库用户名及密码。
*
* @param doc
* @throws Exception
*/
public void parseDBInfo(Document doc, String dbID, String dbUsrName,
String dbPwd) {
Element root = doc.getRootElement();
List dbList = root.getChildren(UsrDef.TAG_DB);
// System.out.println(dbList.size());
for (Iterator iter = dbList.iterator(); iter.hasNext();) {
Element db = (Element) iter.next();
String Id = db.getAttributeValue(UsrDef.ATTR_ID);
if (Id.equals(dbID)) {
db.setAttribute(UsrDef.ATTR_USERNAME, dbUsrName);
db.setAttribute(UsrDef.ATTR_PWD, dbPwd);
saveProperties();
}
if (Id.equals(dbID)) {
db.setAttribute(UsrDef.ATTR_USERNAME, dbUsrName);
db.setAttribute(UsrDef.ATTR_PWD, dbPwd);
saveProperties();
}
}
}
|
|