ITPub博客

首页 > 应用开发 > IT综合 > JDom读写XML(代码)

JDom读写XML(代码)

原创 IT综合 作者:mouqj 时间:2007-09-01 17:56:46 0 删除 编辑
SAXBuilder builder;
Document doc;
Element root;

public ConfigOperater() {
builder = new SAXBuilder();
try {
doc = builder.build(DataUtil.CONFIG_PATH);
root = doc.getRootElement();
}
catch (IOException ex) {
ex.printStackTrace();
}
catch (JDOMException ex) {
ex.printStackTrace();
}
}

//JDom读取XML

public void readConfig() {
List allConfigDB = root.getChildren("TargetDB");
for(int i=0; i Element dbEle = (Element)allConfigDB.get(i);
Database database = new Database(dbEle.getAttributeValue("dbType"),
dbEle.getAttributeValue("id"),dbEle.getAttributeValue("dbIp"),
Integer.parseInt(dbEle.getAttributeValue("dbPort")),dbEle.getAttributeValue("dbName"),
dbEle.getAttributeValue("dbUser"),dbEle.getAttributeValue("dbPwd"),
dbEle.getAttributeValue("dbServer"),dbEle.getAttributeValue("clientLocale"),
dbEle.getAttributeValue("dbLocale"));
DataUtil.ALL_DB.addElement(database);
}
//DataBase db
}

//JDom写入XML

public void writeToConfig() {
Element dbEle = new Element("TargetDB");
dbEle.setAttribute("id", DataUtil.curDB.getDbConName());
dbEle.setAttribute("dbType", DataUtil.curDB.getDBType());
dbEle.setAttribute("dbServer", DataUtil.curDB.getDbServer());
dbEle.setAttribute("dbName", DataUtil.curDB.getDbName());
dbEle.setAttribute("dbUser", DataUtil.curDB.getDbUser());
dbEle.setAttribute("dbPwd", DataUtil.curDB.getDbPwd());
dbEle.setAttribute("dbIp", DataUtil.curDB.getDbIp());
dbEle.setAttribute("dbPort", Integer.toString(DataUtil.curDB.getDbPort()));
dbEle.setAttribute("dbLocale", DataUtil.curDB.getDbLocale());
dbEle.setAttribute("clientLocale", DataUtil.curDB.getClientLocale());

root.addContent(dbEle);
try {
XMLOutputter outPutter = new XMLOutputter();
PrintWriter outWriter = new PrintWriter(new BufferedWriter(new FileWriter(DataUtil.CONFIG_PATH)));
outPutter.output(doc, outWriter);
}
catch (java.io.IOException e) {
e.printStackTrace();
}
}

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11049438/viewspace-967520/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    33
  • 访问量
    116233