首先你需要从http://jdom.org/dist/binary/jdom-1.1.1.zip下载jdom包,然后在eclipse中导入。
建立一个java类,用以下代码:
.. code:: java
import org.jdom.output.*;
import org.jdom.input.*;
import org.jdom.*;
import java.io.*;
import java.util.*;
public class XmlConfigReader {
public static void main(String[] args) {
SAXBuilder builder = new SAXBuilder();
Document read_doc;
try {
read_doc = builder.build("src//sys-conf.xml");
Element stu = read_doc.getRootElement();
List list = stu.getChildren("db-info");
for(int i = 0;i < list.size();i++)
{
Element e = (Element)list.get(i);
String str_name = e.getChildText("driver-name");
String str_url = e.getChildText("url");
System.out.println("-----------------------");
System.out.println("driver-name:" + str_name);
System.out.println("url:" + str_url);
System.out.println("------------------------------");
System.out.println();
}
} catch (JDOMException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
sys-conf.xml文件如下:
.. code:: xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<db-info>
<driver-name>com.mysql.jdbc.Driver</driver-name>
<url>jdbc:mysql://192.168.1.100/drp1?user=root&password=root</url>
</db-info>
</config>
- from the5fire.com
----EOF-----
微信公众号:Python程序员杂谈
微信公众号:Python程序员杂谈