以XPath方式获取XML元素值

下面是一个工具类,以XPath方式获取XML元素值,此工具类于上海期货交易所的一个系统项目中创建,代码如下:

java 代码
  1. /**  
  2.  *   
  3.  */  
  4. package com.test.utils.toolkit;   
  5.   
  6. import java.io.File;   
  7. import java.io.FileNotFoundException;   
  8. import java.net.MalformedURLException;   
  9. import java.util.ArrayList;   
  10. import java.util.Iterator;   
  11. import java.util.List;   
  12.   
  13. import org.dom4j.Document;   
  14. import org.dom4j.DocumentException;   
  15. import org.dom4j.Element;   
  16. import org.dom4j.Node;   
  17. import org.dom4j.io.SAXReader;   
  18.   
  19. /**  
  20.  * @author shjy.nicholas  
  21.  *   
  22.  */  
  23. public class XMLToolkit {   
  24.        
  25.     private String fileName = null ;   
  26.     private Document doc = null ;   
  27.        
  28.     public XMLToolkit(String fileName) throws MalformedURLException, DocumentException, FileNotFoundException {   
  29.         this.fileName = fileName ;   
  30.         this.doc = readDocument() ;   
  31.     }   
  32.        
  33.     private Document readDocument()   
  34.             throws MalformedURLException, DocumentException, FileNotFoundException {   
  35.         SAXReader reader = new SAXReader();   
  36.         Document document = reader.read(new File(this.fileName));   
  37.         return document;   
  38.     }   
  39.   
  40.     public Element getRootElement() {   
  41.         return this.doc.getRootElement();   
  42.     }   
  43.   
  44.     // 获取唯一节点的值   
  45.     public String getNodeValue(String path) {   
  46.         Node node = this.doc.selectSingleNode(path);   
  47.         String value = node.getText() ;   
  48.         return value;   
  49.     }   
  50.        
  51.     // 获取多个同名节点的值   
  52.     public List getNodesValue(String path) {   
  53.         List list = this.doc.selectNodes(path);   
  54.         Iterator it = list.iterator() ;   
  55.         List resultList = new ArrayList() ;   
  56.         while(it.hasNext()) {   
  57.             Node node = (Node)it.next() ;   
  58.             resultList.add(node.getText()) ;   
  59.         }   
  60.         return resultList ;   
  61.     }   
  62.   
  63.     public static void main(String[] args) {   
  64.         try {   
  65.             XMLToolkit xmlTool = new XMLToolkit("home/datasource.xml") ;   
  66.             String value = xmlTool.getNodeValue("//datasources/datasource/driver") ;   
  67.             System.out.println("value: " + value) ;   
  68.         } catch (MalformedURLException e) {   
  69.             e.printStackTrace();   
  70.         } catch (FileNotFoundException e) {   
  71.             e.printStackTrace();   
  72.         } catch (DocumentException e) {   
  73.             e.printStackTrace();   
  74.         }   
  75.            
  76.     }   
  77. }   
元素属性的获取将后续给出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值