<?xml version="1.0" encoding="UTF-8"?> <dataSources> <!-- 定义MySQL数据源 --> <dataSource id="mysql" class="xxx.xxx.xx"> <property name="driverClassName">com.mysql.jdbc.Driver</property> <property name="url">jdbc:mysql://127.0.0.1:3306/userdb</property> <property name="username">root</property> <property name="password">123</property> </dataSource> <!-- 定义Oracle的数据源 --> <dataSource default="true" id="oracle" class="xxx.xxx.xx"> <property name="driverClassName">com.oracle.jdbc.OracleDriver </property> <property name="url">jdbc:oracle:thin:@127.0.0.1:1521:ORCL </property> <property name="username">scott</property> <property name="password">tiger</property> </dataSource> <dataSource id="SQLServer" class="xxx.xxx.xx"> <property name="driverClassName">com.oracle.jdbc.OracleDriver</property> <property name="url">jdbc:oracle:thin:@127.0.0.1:1521:ORCL</property> <property name="username">scott</property> <property name="password">tiger</property> </dataSource> <transaction> </transaction> </dataSources>
使用JDOM解析XML文件
package com.jdom.demo; import java.io.InputStream; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; import org.jdom.xpath.XPath; /** * 使用JDOM解析XML文件 * * @author Administrator 补全快捷键: Ctrl+2, L * */ public class JDOMParser { public static void main(String[] args) throws Exception { //testJdomParser(); testXPathXMLParser("oracle"); } private static void testJdomParser() throws Exception { // 建造者设计模式 SAXBuilder saxBuilder = new SAXBuilder(); // 获取XML文件对应输入流 InputStream in = JDOMParser.class.getClassLoader().getResourceAsStream("datasource.xml"); // 通过构建器对象构建一个文档对象 Document document = saxBuilder.build(in); // 获取根元素 Element rootElement = document.getRootElement(); // 获取根元素下面所有子元素 // getChildren() 获取所有子元素集合 // getChildren("元素名称") // 获取指定元素名称的元素集合 List<Element> childList = rootElement.getChildren("dataSource"); for (Element element : childList) { // 获取id属性值 String id = element.getAttributeValue("id"); String clazz = element.getAttributeValue("class"); System.out.println("id=" + id + "-----class=" + clazz); // 获取所有的property子节点 List<Element> propertyChild = element.getChildren("property"); for (Element childEl : propertyChild) { String name = childEl.getAttributeValue("name"); // 获取元素name属性值 String text = childEl.getTextTrim(); // 获取元素中的内容 System.out.println("name=" + name + "=========content=" + text); } System.out.println("--------------------------"); } } /** * 使用XPath选取节点 * http://www.cnblogs.com/hoojo/archive/2011/08/11/2134638.html XPath解析 */ private static void testXPathXMLParser(String dataSourceId) throws Exception{ // 建造者设计模式 SAXBuilder saxBuilder = new SAXBuilder(); // 获取XML文件对应输入流 InputStream in = JDOMParser.class.getClassLoader().getResourceAsStream("datasource.xml"); // 通过构建器对象构建一个文档对象 Document document = saxBuilder.build(in); // 获取根元素 // Element rootElement = document.getRootElement(); // path: XPath表达式 XPath xpath = XPath.newInstance("dataSources/dataSource[@id='"+dataSourceId+"']"); Element dataSource = (Element) xpath.selectSingleNode(document); // "//" 选取配置元素组成集合,不考虑位置 XPath propXpath = XPath.newInstance("property"); List<Element> nodes = propXpath.selectNodes(dataSource); // 从选择到dataSource元素下面选择子节点 for (Element el : nodes) { System.out.println(el.getAttributeValue("name")+"-------------"+el.getTextTrim()); } } }
dom4j常规方式解析
package com.dom4j.demo; import java.io.InputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.XPath; import org.dom4j.io.SAXReader; public class Dom4jParser { public static void main(String[] args) throws Exception { dom4jXpath("mysql"); } /** * dom4j常规方式解析 -> 使用命名空间方式解析 * @throws Exception */ private static void dom4j() throws Exception { // 创建SaxReader对象 SAXReader reader = new SAXReader(); InputStream in = Dom4jParser.class.getClassLoader().getResourceAsStream("datasource.xml"); // 获取到文档对象 Document document = reader.read(in); // 获取根元素对象 Element rootElement = document.getRootElement(); // System.out.println(rootElement); // 获取根元素下面所有子元素 // List<Element> elements = rootElement.elements(); // 获取根元素下面指定元素名称的子元素集合 List<Element> elements = rootElement.elements("dataSource"); for (Element el : elements) { System.out.println(el.attributeValue("id") + "-----" + el.attributeValue("class")); // 获取el元素下面所有子元素 List<Element> childList = el.elements("property"); for (Element childEl : childList) { String name = childEl.attributeValue("name"); String text = childEl.getTextTrim(); System.out.println(name + "----------" + text); } System.out.println("------------------------------------------"); } } /** * dom4j方式解析使用XPath * @throws Exception */ private static void dom4jXpath(String dataSourceId) throws Exception { // 创建SaxReader对象 SAXReader reader = new SAXReader(); InputStream in = Dom4jParser.class.getClassLoader().getResourceAsStream("datasource.xml"); // 获取到文档对象 Document document = reader.read(in); XPath xpath = document.createXPath("dataSources/dataSource[@id='"+dataSourceId+"']"); Element dataSourceNode = (Element) xpath.selectSingleNode(document); System.out.println(dataSourceNode.attributeValue("id")+"----"+dataSourceNode.attributeValue("class")); xpath = document.createXPath("property"); List<Element> propList = xpath.selectNodes(dataSourceNode); for (Element el : propList) { String name = el.attributeValue("name"); String text = el.getTextTrim(); System.out.println(name+"--------------"+text); } } }