Introduce xpath documention

XPath是解析xml文件的一項技術標準,這段時間工作需要,所以學習了下,下面是學習的要點及使用dom4j解析的實現,有興趣的可以參考
[b]XPath學習的要點:[/b]
1、XPath 節點
2、XPath 語法
3、XPath Axes(座標軸)
4、XPath 運算符
5、XPath 實例

[b]XPath介紹文檔:[/b]
http://www.w3school.com.cn/xpath/xpath_syntax.asp
[b]以下是讀取xml文件的一個實現[/b]

/*
* Created on Jun 20, 2008
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.chris.demo.xpath;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

/**
* @author Chris.wang
* Window - Preferences - Java - Code Style - Code Templates
*/
public class XMLHandler {
private static Document doc = null;
/**
* readXML from disk by filename
* @param String filename
* @throws FileNotFoundException
*/
public static void readXML(String filename) throws FileNotFoundException{
File f = new File(filename);
if(f.exists()==false){
throw new FileNotFoundException(filename+" not found");
}
InputStream in = new FileInputStream(filename);

SAXReader reader = new SAXReader();
try {
doc=reader.read(in);
} catch (DocumentException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
/**
* @return Returns the doc.
*/
public static Document getDoc() {
return doc;
}
}

[b]以下是解析一段xml文件的方法[/b]

/*
* Created on Jun 20, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.chris.demo.xpath;

import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;
import junit.framework.Assert;
import org.dom4j.Document;
import org.dom4j.Element;
/**
* XPathTester
* @author Chris.wang
*/
public class XPathTester {
/**
* test xpath
* @author chris
*/
public void test(){
String filename = "D:\\working\\SOAP request.xml";
try {
XMLHandler.readXML(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
final Document doc = XMLHandler.getDoc();
Assert.assertTrue(doc!=null);
Element root = doc.getRootElement();
Assert.assertTrue(root!=null);
// XPath pathParser = new DefaultXPath("task");
// System.out.println(root.elements().size());
List list = root.selectNodes("//suit");
System.out.println("nodes size = "+ list.size());
for(Iterator iter = list.iterator();iter.hasNext();){
Element elem = (Element)iter.next();
System.out.println(elem.attributeValue("suit-name"));
}
}
public static void main(String[] args) {
XPathTester tester = new XPathTester();
tester.test();
}
}

[b]以下是被解析的xml文件:[/b]
<?xml version="1.0" encoding="UTF-8"?>
<tree>
<task task-name="fundation test">
<suit suit-name="booking test">
<case case-name="browse">
hello world
</case>
<case case-name="update">
</case>
</suit>
<suit suit-name="filghting test">
</suit>
</task>
</tree>
[b]結果輸出:[/b]
nodes size = 2
booking test
filghting test
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章