dom4j比如alibaba - 凯时娱乐

dom4j比如alibaba

2019-02-09 07:50:25 | 作者: 昆纬 | 标签: 节点,特点,示例 | 浏览: 6356

/**
  * 解析包括有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" 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" 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;
 }

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表凯时娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章

阅读排行

  • 1

    第八章 装修形式环球

    形式,组件,结构
  • 2

    CreateThread()、mingxing

    线程,函数,运转
  • 3

    js 目标过错itjob

    目标,网站,上线
  • 4
  • 5
  • 6

    dom4j比如alibaba

    节点,特点,示例
  • 7

    没事瞎玩一下dongfang

    蒲公英,程序,代码
  • 8
  • 9
  • 10