`
谷超
  • 浏览: 163951 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

jdom读取xml

    博客分类:
  • Xml
阅读更多

config.xml文档

 

<?xml version="1.0" encoding="UTF-8"?>
<configure>
	<driver-class-name>oracle.jdbc.driver.OracleDriver</driver-class-name>
	<url>jdbc:oracle:thin:@127.0.0.1:1521:ora9i</url>
	<username>xsfw</username>
	<password>xsfw</password>
</configure>

 

 读取xml文档,利用xpath,可见学习好xpath对于操作xml是多么的重要啊!

 

package test.jdom;

import java.io.IOException;

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

public class readXML {

	public static void main(String[] args) {
		SAXBuilder sb=new SAXBuilder();
		try {
			Document doc=sb.build("config.xml");
			Element root=doc.getRootElement();
			Element driverNameElt=(Element)XPath.selectSingleNode(root,"//configure/driver-class-name");
			String driver=driverNameElt.getText();
			
			Element urlElt=(Element)XPath.selectSingleNode(root,"//configure/url");
			String url=urlElt.getText();
			
			Element userNameElt=(Element)XPath.selectSingleNode(root,"//configure/username");
			String username=userNameElt.getText();
			
			Element passwordElt=(Element)XPath.selectSingleNode(root,"//configure/password");
			String pwd=passwordElt.getText();
			
			System.out.println(driver);
			System.out.println(url);
			System.out.println(username);
			System.out.println(pwd);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics