论坛首页 入门技术论坛

Introduce xpath documention

浏览 1894 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-06-26  
XPath是解析xml文件的一项技术标准,这段时间工作需要,所以学习了下,下面是学习的要点及使用dom4j解析的实现,有兴趣的可以参考
XPath学习的要点:
1、XPath 节点
2、XPath 语法
3、XPath Axes(坐标轴)
4、XPath 运算符
5、XPath 实例

XPath介绍文档:
http://www.w3school.com.cn/xpath/xpath_syntax.asp
以下是读取xml文件的一个实现
/*
 * 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;
	}
}

以下是解析一段xml文件的方法
/*
 * 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();
	}
}

以下是被解析的xml文件:
<?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>
结果输出:
nodes size = 2
booking test
filghting test
   发表时间:2009-01-05  
very good
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics