/**
* 解析包含有DB连接信息的XML文件
* 格式必须符合如下规范:
* 1. 最多三级,每级的node名称自定义;
* 2. 二级节点支持节点属性,属性将被视作子节点;
* 3. CDATA必须包含在节点中,不能单独出现。
*
* 示例1——三级显示:
* <db-connections>
* <connection>
* <name>DBTest</name>
* <jndi></jndi>
* <url>
* <![CDATA[jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF8]]>
* </url>
* <driver>org.gjt.mm.mysql.Driver</driver>
* <user>test</user>
* <password>test2012</password>
* <max-active>10</max-active>
* <max-idle>10</max-idle>
* <min-idle>2</min-idle>
* <max-wait>10</max-wait>
* <validation-query>SELECT 1+1</validation-query>
* </connection>
* </db-connections>
*
* 示例2——节点属性:
* <bookstore>
* <book category="cooking">
* <title lang="en">Everyday Italian</title>
* <author>Giada De Laurentiis</author>
* <year>2005</year>
* <price>30.00</price>
* </book>
*
* <book category="children" title="Harry Potter" author="J K. Rowling" year="2005" price="$29.9"/>
* </bookstore>
*
* @param configFile
* @return
* @throws Exception
*/
public static List<Map<String, String>> parseDBXML(String configFile) throws Exception {
List<Map<String, String>> dbConnections = new ArrayList<Map<String, String>>();
InputStream is = Parser.class.getResourceAsStream(configFile);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(is);
Element connections = document.getRootElement();
Iterator<Element> rootIter = connections.elementIterator();
while (rootIter.hasNext()) {
Element connection = rootIter.next();
Iterator<Element> childIter = connection.elementIterator();
Map<String, String> connectionInfo = new HashMap<String, String>();
List<Attribute> attributes = connection.attributes();
for (int i = 0; i < attributes.size(); ++i) { // 添加节点属性
connectionInfo.put(attributes.get(i).getName(), attributes.get(i).getValue());
}
while (childIter.hasNext()) { // 添加子节点
Element attr = childIter.next();
connectionInfo.put(attr.getName().trim(), attr.getText().trim());
}
dbConnections.add(connectionInfo);
}
return dbConnections;
}
/**
* 解析包含有DB连接信息的XML文件
* 格式必须符合如下规范:
* 1. 最多三级,每级的node名称自定义;
* 2. 二级节点支持节点属性,属性将被视作子节点;
* 3. CDATA必须包含在节点中,不能单独出现。
*
* 示例1——三级显示:
* <db-connections>
* <connection>
* <name>DBTest</name>
* <jndi></jndi>
* <url>
* <![CDATA[jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF8]]>
* </url>
* <driver>org.gjt.mm.mysql.Driver</driver>
* <user>test</user>
* <password>test2012</password>
* <max-active>10</max-active>
* <max-idle>10</max-idle>
* <min-idle>2</min-idle>
* <max-wait>10</max-wait>
* <validation-query>SELECT 1+1</validation-query>
* </connection>
* </db-connections>
*
* 示例2——节点属性:
* <bookstore>
* <book category="cooking">
* <title lang="en">Everyday Italian</title>
* <author>Giada De Laurentiis</author>
* <year>2005</year>
* <price>30.00</price>
* </book>
*
* <book category="children" title="Harry Potter" author="J K. Rowling" year="2005" price="$29.9"/>
* </bookstore>
*
* @param configFile
* @return
* @throws Exception
*/
public static List<Map<String, String>> parseDBXML(String configFile) throws Exception {
List<Map<String, String>> dbConnections = new ArrayList<Map<String, String>>();
InputStream is = Parser.class.getResourceAsStream(configFile);
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(is);
Element connections = document.getRootElement();
Iterator<Element> rootIter = connections.elementIterator();
while (rootIter.hasNext()) {
Element connection = rootIter.next();
Iterator<Element> childIter = connection.elementIterator();
Map<String, String> connectionInfo = new HashMap<String, String>();
List<Attribute> attributes = connection.attributes();
for (int i = 0; i < attributes.size(); ++i) { // 添加节点属性
connectionInfo.put(attributes.get(i).getName(), attributes.get(i).getValue());
}
while (childIter.hasNext()) { // 添加子节点
Element attr = childIter.next();
connectionInfo.put(attr.getName().trim(), attr.getText().trim());
}
dbConnections.add(connectionInfo);
}
return dbConnections;
}
相关推荐
Dom4j例子 Dom4j例子 Dom4j例子 解析大部分的XML
Dom4j完成例子 Dom4j完成例子 Dom4j完成例子
综合他人的dom4j例子
dom4j最新例子与介绍,xml开发的首选
DOM4j教程 例子 DOM4j教程 例子 DOM4j教程 例子 DOM4j教程 例子
这里有很多dom4j解析的小例子,结合实例看看,就能懂得!
用dom4j操作xml的例子,基本的增删改查都有。
介绍DOM4J的主要接口的使用方法,Elementorg.dom4j.Attribute : Attribute接口定义了XML文件的属性 org.dom4j.Branch:Branch为能够包含子节点的节点如XML元素和文档定义了一个公共为 org.dom4j.CDATA : CDATA定义了...
例子演示了3个功能 1、创建、读取XML 2、XML通过XSL转换成HTML 3、验证XSL
dom4j的例子,很简单的dom4j.里面有dom4j的配置文件
dom4j的例子 xpp3 dom4j说明 visitor解析
DOM4j和log4j简单例子详解,综合网上例子,和大家一起分享。导入Eclipse中就可。谢谢!
dom4j的源文件(包含一些例子和jar包)dom4j-1.6.1
dom4j最简单的小例子,dom4j最简单的小例子,dom4j最简单的小例子,dom4j最简单的小例子
自己写的一个dom4j的例子,xml文件和java源码都打包在了一起,内容包括基本的增删改查、
读取文档中用到的dom4j_例子,包括所有常用用法
使用java编写,spring框架,读取数据库中内容使用dom4j包生成XML文档,并包括一个Transformer使用Xslt转换xml的示例
dom4j关于xml读写的例子里面还有dom4j的jar包,对初学者很有帮助
Dom4j解析xml与配置文件的小例子,简单的Dom4j的小应用。
dom4j 包含例子 各种增删改查