在使用Dom4j解析xml文檔時,我們很希望有一種類似正則表達式的東西來規范查詢條件,而xpath正是這樣一種很便利的規則.
String xmlName = path + "/" + userName + ".xml";
// 定義需要返回的第一級菜單的名字集合
List firstNames = new ArrayList();
// Attribute的屬性集合
List attrs = new ArrayList();
// 聲明SAXReader
SAXReader saxReader = new SAXReader();
try {
Document doc = saxReader.read(xmlName);
// 獲得所有grade=1的Element的text的值
String xpath = "/tree/item";
List list = doc.selectNodes(xpath);
Iterator it = list.iterator();
while (it.hasNext()) {
Element elt = (Element) it.next();
Attribute attr = elt.attribute("grade");
System.out.println(attr.getValue());
if (new Integer(attr.getValue()).intValue() == 1) {
attr = elt.attribute("text");
attrs.add(attr.getValue());
System.out.println(attr.getValue());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
return attrs;
String xmlName = path + "/" + userName + ".xml";
// 定義需要返回的第一級菜單的名字集合
List firstNames = new ArrayList();
// Attribute的屬性集合
List attrs = new ArrayList();
// 聲明SAXReader
SAXReader saxReader = new SAXReader();
try {
Document doc = saxReader.read(xmlName);
// 獲得所有grade=1的Element的text的值
String xpath = "/tree/item";
List list = doc.selectNodes(xpath);
Iterator it = list.iterator();
while (it.hasNext()) {
Element elt = (Element) it.next();
Attribute attr = elt.attribute("grade");
System.out.println(attr.getValue());
if (new Integer(attr.getValue()).intValue() == 1) {
attr = elt.attribute("text");
attrs.add(attr.getValue());
System.out.println(attr.getValue());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
return attrs;
還有一個是獲取某個節點下面里的所有第一級子節點,而不是所有的節點(包括子節點和孫節點).
public static List getSecondMenuNames(String textName, String path,
String userName) {
String xmlName = path + "/" + userName + ".xml";
String name = textName;
// 定義需要返回的第二級菜單的名字集合
List firstNames = new ArrayList();
// Attribute的屬性集合
List attrs = new ArrayList();
// 聲明SAXReader
SAXReader saxReader = new SAXReader();
try {
Document doc = saxReader.read(xmlName);
// 這個xpath的意思是,獲取text='系統管理'的一個Item下的所有Item的節點
String xpath = "//item[@text='" + name + "']/child::*";
List list = doc.selectNodes(xpath);
Iterator it = list.iterator();
while (it.hasNext()) {
Element elt = (Element) it.next();
Attribute attr = elt.attribute("grade");
System.out.println(attr.getValue());
attr = elt.attribute("text");
System.out.println(attr.getValue());
attrs.add(attr.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
return attrs;
}
public static List getSecondMenuNames(String textName, String path,
String userName) {
String xmlName = path + "/" + userName + ".xml";
String name = textName;
// 定義需要返回的第二級菜單的名字集合
List firstNames = new ArrayList();
// Attribute的屬性集合
List attrs = new ArrayList();
// 聲明SAXReader
SAXReader saxReader = new SAXReader();
try {
Document doc = saxReader.read(xmlName);
// 這個xpath的意思是,獲取text='系統管理'的一個Item下的所有Item的節點
String xpath = "//item[@text='" + name + "']/child::*";
List list = doc.selectNodes(xpath);
Iterator it = list.iterator();
while (it.hasNext()) {
Element elt = (Element) it.next();
Attribute attr = elt.attribute("grade");
System.out.println(attr.getValue());
attr = elt.attribute("text");
System.out.println(attr.getValue());
attrs.add(attr.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
return attrs;
}
注意看其中的xpath的寫法,正是因為有了xpath,我們才能如此簡單靈活的對xml進行操作.
剛剛使用xpath的時候可能會報一個錯誤:Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
這時我們應該往CLASSPATH導入一個jar包,叫jaxen-1.1.1.jar,可從網上下載.
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
表達式 描述
節點名 選擇所有該名稱的節點集
/ 選擇根節點
// 選擇當前節點下的所有節點
. 選擇當前節點
.. 選擇父節點
@ 選擇屬性
示例
表達式 描述
bookstore 選擇所有bookstore子節點
/bookstore 選擇根節點bookstore
bookstore/book 在bookstore的子節點中選擇所有名為book的節點
//book 選擇xml文檔中所有名為book的節點
bookstore//book 選擇節點bookstore下的所有名為book為節點
//@lang 選擇所有名為lang的屬性
表達式 描述
/bookstore/book[1] 選擇根元素bookstore的book子元素中的第一個(注意: IE5以上瀏覽器中第一個元素是0)
/bookstore/book[last()] 選擇根元素bookstore的book子元素中的最后一個
/bookstore/book[last()-1] 選擇根元素bookstore的book子元素中的最后第二個
/bookstore/book[position()35.00] 選擇根元素bookstore的book子元素中那些擁有price子元素且值大于35的
/bookstore/book[price>35.00]/title 選擇根元素bookstore的book子元素中那些擁有price子元素且值大于35的title子元素
通配符 描述
* 匹配所有元素
@* 匹配所有屬性節點
node() 匹配任何類型的節點
示例
表達式 描述
/bookstore/* 選擇根元素bookstore的下的所有子元素
//* 選擇文檔中所有元素
//title[@*] 選擇所有擁有屬性的title元素
posted on 2009-05-19 00:12
lvq810 閱讀(606)
評論(0) 編輯 收藏 所屬分類:
Open Framekwork