当前位置: 代码迷 >> J2SE >> java写xml解决方案
  详细解决方案

java写xml解决方案

热度:6146   发布时间:2013-02-25 00:00:00.0
java写xml
用java写xml文件,如何实现如下功能:
比如有一个完整的xml文件,现在要用程序修改里面的内容,但不能重新生成一遍文件,只是想修改改动过的地方,请问如何实现?

就像在MyEclipse中用Struts时,struts的图形和相应的source是相对应的,改变了图形相应的代码也改变了。



------解决方案--------------------------------------------------------
package 读取XML;

public class JdbcInfo {

private String driverClassName;

private String url;

private String userName;

private String password;

public String getDriverClassName() {
return driverClassName;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
public String toString(){
return "driverClassName="+driverClassName+
",url="+url+
",userName="+userName+
",password="+password;
}

public void setDriverClassName(String text) {
this.driverClassName = text;
}

}
package 读取XML;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

import sun.text.CompactShortArray.Iterator;
import 次方.Impl;

public class GloabalConfigure {

private static GloabalConfigure instance = new GloabalConfigure();

private static final String CONFIG_FILE_NAME = "MyXML.XML";

private JdbcInfo jdbcInfo = new JdbcInfo();

private Map beanMap = new HashMap();
private Element rootElt;

private GloabalConfigure(){
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_FILE_NAME));
this.rootElt = doc.getRootElement();
initJdbcInfo();
try {
initBean();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static GloabalConfigure getInstance(){
return instance;
}
private void initJdbcInfo(){
try {
Element driverClassNameElt = (Element)XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/driver-class-name");
//System.out.println(driverClassNameElt.getText());
jdbcInfo.setDriverClassName(driverClassNameElt.getText());

Element urlElt = (Element)XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/url");
jdbcInfo.setUrl(urlElt.getText());


Element userElt = (Element)XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/user-name");
jdbcInfo.setUserName(userElt.getText());


Element passwordElt = (Element)XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/password");
jdbcInfo.setPassword(passwordElt.getText());

} catch (JDOMException e) {
e.printStackTrace();
}
}

private void initBean() throws InstantiationException, IllegalAccessException, ClassNotFoundException{
  相关解决方案