dom4j解析xml字符串实例

来源:互联网 发布:mysql分页查询语句 编辑:程序博客网 时间:2024/05/01 19:44
dom4j解析xml字符串实例

DOM4J

    与利用DOM、SAX、JAXP机制来解析xml相比,DOM4J 表现更优秀,具有性能优异、功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml。dom4j是一套开源的api。实际项目中,往往选择dom4j来作为解析xml的利器。

先来看看dom4j中对应XML的DOM树建立的继承关系

针对于XML标准定义,对应于图2-1列出的内容,dom4j提供了以下实现:

同时,dom4j的NodeType枚举实现了XML规范中定义的node类型。如此可以在遍历xml文档的时候通过常量来判断节点类型了。

常用API

class org.dom4j.io.SAXReader

  • read  提供多种读取xml文件的方式,返回一个Domcument对象

interface org.dom4j.Document

  • iterator  使用此法获取node
  • getRootElement  获取根节点

interface org.dom4j.Node

  • getName  获取node名字,例如获取根节点名称为bookstore
  • getNodeType  获取node类型常量值,例如获取到bookstore类型为1——Element
  • getNodeTypeName  获取node类型名称,例如获取到的bookstore类型名称为Element

interface org.dom4j.Element

  • attributes  返回该元素的属性列表
  • attributeValue  根据传入的属性名获取属性值
  • elementIterator  返回包含子元素的迭代器
  • elements  返回包含子元素的列表

interface org.dom4j.Attribute

  • getName  获取属性名
  • getValue  获取属性值

interface org.dom4j.Text

  • getText  获取Text节点值

interface org.dom4j.CDATA

  • getText  获取CDATA Section值

interface org.dom4j.Comment

  • getText  获取注释

实例一:

按 Ctrl+C 复制代码
按 Ctrl+C 复制代码

  

实例二:

复制代码
 1 /** 2  * 解析包含有DB连接信息的XML文件 3  * 格式必须符合如下规范: 4  * 1. 最多三级,每级的node名称自定义; 5  * 2. 二级节点支持节点属性,属性将被视作子节点; 6  * 3. CDATA必须包含在节点中,不能单独出现。 7  * 8  * 示例1——三级显示: 9  * <db-connections>10  *         <connection>11  *            <name>DBTest</name>12  *            <jndi></jndi>13  *            <url>14  *                <![CDATA[jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF8]]>15  *             </url>16  *            <driver>org.gjt.mm.mysql.Driver</driver>17  *             <user>test</user>18  *            <password>test2012</password>19  *            <max-active>10</max-active>20  *            <max-idle>10</max-idle>21  *            <min-idle>2</min-idle>22  *            <max-wait>10</max-wait>23  *            <validation-query>SELECT 1+1</validation-query>24  *         </connection>25  * </db-connections>26  *27  * 示例2——节点属性:28  * <bookstore>29  *         <book category="cooking">30  *            <title lang="en">Everyday Italian</title>31  *            <author>Giada De Laurentiis</author>32  *            <year>2005</year>33  *            <price>30.00</price>34  *         </book>35  *36  *         <book category="children" title="Harry Potter" author="J K. Rowling" year="2005" price="$29.9"/>37  * </bookstore>38  *39  * @param configFile40  * @return41  * @throws Exception42  */43 public static List<Map<String, String>> parseDBXML(String configFile) throws Exception {44     List<Map<String, String>> dbConnections = new ArrayList<Map<String, String>>();45     InputStream is = Parser.class.getResourceAsStream(configFile);46     SAXReader saxReader = new SAXReader();47     Document document = saxReader.read(is);48     Element connections = document.getRootElement();49 50     Iterator<Element> rootIter = connections.elementIterator();51     while (rootIter.hasNext()) {52         Element connection = rootIter.next();53         Iterator<Element> childIter = connection.elementIterator();54         Map<String, String> connectionInfo = new HashMap<String, String>();55         List<Attribute> attributes = connection.attributes();56         for (int i = 0; i < attributes.size(); ++i) { // 添加节点属性57             connectionInfo.put(attributes.get(i).getName(), attributes.get(i).getValue());58         }59         while (childIter.hasNext()) { // 添加子节点60             Element attr = childIter.next();61             connectionInfo.put(attr.getName().trim(), attr.getText().trim());62         }63         dbConnections.add(connectionInfo);64     }65 66     return dbConnections;67 }