Java读取Xml

public void done() {
File xmlfile = new File((new File("").getAbsolutePath())
+ "/WebRoot/DBconfig.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Document Doc = null;
try {
Doc = builder.parse(xmlfile);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Driver = Doc.getElementsByTagName("driver").item(0).getFirstChild()
.getNodeValue();
URL = Doc.getElementsByTagName("URL").item(0).getFirstChild()
.getNodeValue();
username = Doc.getElementsByTagName("username").item(0).getFirstChild()
.getNodeValue();
pwd = Doc.getElementsByTagName("pwd").item(0).getFirstChild()
.getNodeValue();
testsql = Doc.getElementsByTagName("testsql").item(0).getFirstChild()
.getNodeValue();
System.out.println("Driver:->" + Driver + "\nURL:->" + URL
+ "\n用户名:->" + username + "\n密码:->" + pwd + "\ntestsql:"
+ testsql);
}










<?xml version="1.0" encoding="utf-8"?>
<config>
<!--连接数据库的驱动程序-->
<driver>oracle.jdbc.driver.OracleDriver</driver>
<URL>jdbc:oracle:thin:@127.0.0.1:1521:ZWM</URL>
<!--用 户 名-->
<username>a</username>
<!--密 码-->
<pwd>a</pwd>
<!--测试sql-->
<testsql>select sysdate from dual</testsql>
</config>

你可能感兴趣的:(java,oracle,sql,xml,jdbc)