1、xml文件
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<something-else-entirely>
<proxool>
<alias>hibernate-sqlserver</alias>
<driver-url>jdbc:jtds:sqlserver://127.0.0.1:1433/t4</driver-url>
<driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="sa"/>
<property name="password" value="database"/>
</driver-properties>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<prototype-count>5</prototype-count>
<maximum-connection-count>100</maximum-connection-count>
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>
2、java解析源代码
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XMLUtil {
public static Document read(File file, String url, String driver,
String username, String password) throws SAXException, IOException,
ParserConfigurationException {
if (file == null || file.isDirectory() || !file.exists()) {
throw new RuntimeException("xml文件不存或者为空");
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
factory.setIgnoringComments(true);
Document doc = builder.parse(file);
Element root = doc.getDocumentElement();
NodeList childrenList = root.getChildNodes();
for (int j = 0; j < childrenList.getLength(); j++) {
NodeList proxoolChildrenList = childrenList.item(j).getChildNodes();
for (int i = 0; i < proxoolChildrenList.getLength(); i++) {
Node ele = proxoolChildrenList.item(i);
if (ele.getNodeName().equals("driver-url")) {
ele.setTextContent(url);
}
if (ele.getNodeName().equals("driver-class") && driver != null) {
ele.setTextContent(driver);
}
if (ele.getNodeName().equals("driver-properties")) {
NodeList infoList = ele.getChildNodes();
for (int index = 0; index < infoList.getLength(); index++) {
ele = infoList.item(index);
NamedNodeMap nnm = ele.getAttributes();
if(nnm == null){
continue;
}
Node node = nnm.getNamedItem("name");
if(node == null){
continue;
}
if (node.getNodeValue().equals("user")) {
nnm.getNamedItem("value").setNodeValue(username);
}
if (node.getNodeValue().equals("password")) {
nnm.getNamedItem("value").setNodeValue(password);
}
}
}
}
}
return doc;
}
public static void write(Document doc, File file) {
try {
doc.normalize();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer former = tf.newTransformer();
former.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
former.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
PrintWriter out = new PrintWriter(new FileOutputStream(file));
StreamResult result = new StreamResult(out);
former.transform(source, result);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
String f = "E:/test/proxool.xml";
String t = "E:/test/new_proxool.xml";
try {
Document doc = read(new File(f), "url", "driver", "username","password");
write(doc, new File(t));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
}