使用Dom4j解析包含有DB连接信息的XML文件以及节点属性的获取

包含DB连接信息的XML文件

 1 <!--示例1——三级显示-->

 2 <db-connections>

 3     <connection>

 4         <name>DBTest</name>

 5         <jndi></jndi>

 6         <url>

 7             <![CDATA[jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF8]]>

 8         </url>

 9         <driver>org.gjt.mm.mysql.Driver</driver>

10         <user>test</user>

11         <password>test2012</password>

12         <max-active>10</max-active>

13         <max-idle>10</max-idle>

14         <min-idle>2</min-idle>

15         <max-wait>10</max-wait>

16         <validation-query>SELECT 1+1</validation-query>

17     </connection>

18 </db-connections>
db-connections.xml

节点属性XML文件

 1 <!--示例2——节点属性-->

 2 <bookstore>

 3         <book category="cooking">

 4            <title lang="en">Everyday Italian</title>

 5            <author>Giada De Laurentiis</author>

 6            <year>2005</year>

 7            <price>30.00</price>

 8         </book>

 9         <book category="children" title="Harry Potter" author="J K. Rowling" year="2005" price="$29.9"/>

10 </bookstore>
bookstore.xml

解析代码:

 1 package xml;

 2 

 3 import java.io.File;

 4 import java.io.FileInputStream;

 5 import java.util.ArrayList;

 6 import java.util.HashMap;

 7 import java.util.Iterator;

 8 import java.util.List;

 9 import java.util.Map;

10 

11 import org.dom4j.Attribute;

12 import org.dom4j.Document;

13 import org.dom4j.Element;

14 import org.dom4j.io.SAXReader;

15 

16 public class TestDom4j2 {

17 

18     /**

19      * 解析包含有DB连接信息的XML文件

20      * 格式必须符合如下规范:

21      * 1. 最多三级,每级的node名称自定义;

22      * 2. 二级节点支持节点属性,属性将被视作子节点;

23      * 3. CDATA必须包含在节点中,不能单独出现。

24      */

25     public static List<Map<String, String>> parseDBXML(String configFile) throws Exception {

26         List<Map<String, String>> dbConnections = new ArrayList<Map<String, String>>();

27         //从JAVA的项目路径中去查询文件,文件必须在src源码包路径下才可以使用该方法

28         //InputStream is = TestDom4j2.class.getResourceAsStream(configFile);

29         

30         //从盘符中获取xml文件的内容

31         FileInputStream is = new FileInputStream(new File(configFile));

32         SAXReader saxReader = new SAXReader();

33         Document document = saxReader.read(is);

34         //获取根节点db-connections或bookstore

35         Element connections = document.getRootElement();

36 

37         Iterator<Element> rootIter = connections.elementIterator();

38         while (rootIter.hasNext()) {

39             Element connection = rootIter.next();

40             Iterator<Element> childIter = connection.elementIterator();

41             

42             Map<String, String> connectionInfo = new HashMap<String, String>();

43             //获取connection或book节点的所有属性

44             List<Attribute> attributes = connection.attributes();

45             //遍历节点属性

46             for (int i = 0; i < attributes.size(); ++i) { 

47                 //添加节点属性值

48                 connectionInfo.put(attributes.get(i).getName(), attributes.get(i).getValue());

49             }

50             //遍历connection或book的子节点

51             while (childIter.hasNext()) { 

52                 //添加子节点

53                 Element attr = childIter.next();

54                 connectionInfo.put(attr.getName().trim(), attr.getText().trim());

55             }

56             dbConnections.add(connectionInfo);

57         }

58         return dbConnections;

59     }

60     

61     public static void main(String[] args) {

62           try {

63             List<Map<String, String>>  dbList= parseDBXML("E://xml//db-connections.xml");

64             System.out.println("URL=" + dbList.get(0).get("url"));

65             

66             System.out.println();

67             

68             List<Map<String, String>>  list= parseDBXML("E://xml//bookstore.xml");

69             for(int i=0;i<list.size();i++){

70                 System.out.println("title=" + list.get(i).get("title"));

71                 System.out.println("author=" + list.get(i).get("author"));

72                 System.out.println("year=" + list.get(i).get("year"));

73                 System.out.println("price=" + list.get(i).get("price"));

74             }

75         } catch (Exception e) {

76             e.printStackTrace();

77         }

78     }

79 }

运行结果:

URL=jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF8



title=Everyday Italian

author=Giada De Laurentiis

year=2005

price=30.00

title=Harry Potter

author=J K. Rowling

year=2005

price=$29.9

 

你可能感兴趣的:(dom4j)