xml技术之xpath,通过Xpath快速获取xml中的节点内容

  1 package com.chen.xpath;
  2 
  3 import java.io.File;
  4 import java.io.IOException;
  5 import java.util.List;
  6 
  7 import org.jdom.Document;
  8 import org.jdom.Element;
  9 import org.jdom.JDOMException;
 10 import org.jdom.input.SAXBuilder;
 11 import org.jdom.xpath.XPath;
 12 
 13 public class TextXpath
 14 {
 15  private String pathString = "";
 16  public TextXpath(String pathString){
 17   this.pathString = pathString;
 18   
 19  }
 20  
 21  public void selectSome() throws JDOMException, IOException
 22  {
 23   File file = new File(pathString);
 24   if (!file.exists())
 25   {
 26    System.out.println("文件不存在");
 27   }
 28   SAXBuilder sax = new SAXBuilder(false);
 29   Document doc = sax.build(file);
 30   Element rootElement = doc.getRootElement();
 31 //  List list = rootElement.getChildren();
 32 //  this.readXml(list);
 33   // 查询绝对路径/metadata/MODULE/MODULE/OPERATE下的节点集合(包括根路径)
 34   String xpathString = "/metadata/MODULE/MODULE/OPERATE";
 35   List elist = XPath.selectNodes(rootElement, xpathString);
 36   this.readXmlList(elist);
 37   System.out.println("************************************************************");
 38 
 39   // 查询所有节点名字为OPERATE的节点集合
 40   String xpathStringA = "//OPERATE";
 41   List elistA = XPath.selectNodes(rootElement, xpathStringA);
 42   this.readXmlList(elistA);
 43   System.out.println("************************A************************************");
 44   
 45   // 查询所有节点名字为OPERATE中KEY="hr_chenYearAction_list"的节点集合
 46   String xpathStringB = "//OPERATE[@KEY='hr_chenYearAction_list']";
 47   List elistB = XPath.selectNodes(rootElement, xpathStringB);
 48   this.readXmlList(elistB);
 49   System.out.println("*************************B***********************************");
 50 
 51   // 查询所有节点中KEY="hr_chenYearAction_list"的节点集合
 52   String xpathStringC = "//*[@KEY='hr_chenYearAction_list']";
 53   List elistC = XPath.selectNodes(rootElement, xpathStringC);
 54   this.readXmlList(elistC);
 55   System.out.println("*************************C***********************************");
 56 
 57   // 使用 selectSingleNode 查询所有节点中 得到第一个节点 Element
 58   String xpathStringD = "//MODULE/*";
 59   Element element = (Element) XPath.selectSingleNode(rootElement, xpathStringD);
 60   System.out.println(element.getAttributeValue("KEY").toString());
 61   System.out.println(element.getTextNormalize());
 62 //  this.readXmlList(elistD);
 63   System.out.println("***********************D*************************************");
 64   
 65 
 66   /*
 67   XPATH中一些有用的函数:
 68   string concat (string, string, string*)  联接两个字符串 
 69   boolean starts-with (string, string)  判断某字符串是否以另一字符串开头 
 70   boolean contains (string, string)  判断某字符串是否包含另一字符串 
 71   string substring (string, number, number)  取子字符串 
 72   number string-length (string)  测字符串长度 
 73   number sum (node-set)  求和 
 74   number floor (number)  求小于此数的最大整数值 
 75   number ceiling (number)  求大于此数最小整数值 
 76    * 
 77    */
 78   // 查询所有节点中KEY以hr_chenYearAction开头的节点集合
 79   String xpathStringE = "//*[starts-with(@KEY,'hr_chenYearAction')]";
 80   List elistE = XPath.selectNodes(rootElement, xpathStringE);
 81   this.readXmlList(elistE);
 82   System.out.println("*************************E***********************************");
 83   
 84  }
 85  
 86  /**打印结果
 87   * 
 88   * @author chen_weixian
 89   * May 4, 2012   10:53:39 AM
 90   * @param list
 91   */
 92  public void readXmlList(List list)
 93  {
 94   for (int i = 0; i<list.size(); i++ )
 95   {
 96    Element el  = (Element) list.get(i);
 97    System.out.println(el.getAttributeValue("KEY"));
 98    System.out.println(el.getAttributeValue("TITLE"));    
 99   
100   }
101  }
102  
103  public void readXml(List list)
104  {
105   for (int i = 0; i<list.size(); i++ )
106   {
107    Element el  = (Element) list.get(i);
108    List listi = el.getChildren();
109    if (listi != null && listi.size() > 0)
110    {
111     readXml(listi);
112    }
113    else
114    {
115     System.out.println(el.getAttributeValue("KEY"));
116     System.out.println(el.getAttributeValue("TITLE"));    
117    }
118    
119   }
120  }
121 
122  /**
123   * @author chen_weixian
124   * May 4, 2012   9:06:12 AM
125   * @param args
126   */
127  public static void main(String[] args)
128  {
129   String pathString = "F:/workspaceA/jzProject/config/resource_shop.xml";
130   TextXpath textXpath = new TextXpath(pathString);
131   try
132   {
133    textXpath.selectSome();
134   }
135   catch (JDOMException e)
136   {
137    // TODO Auto-generated catch block
138    e.printStackTrace();
139   }
140   catch (IOException e)
141   {
142    // TODO Auto-generated catch block
143    e.printStackTrace();
144   }
145  }
146 
147 }
原文地址:https://www.cnblogs.com/a393060727/p/2942283.html