`
wj98127
  • 浏览: 264850 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

xapth应用

    博客分类:
  • xml
XML 
阅读更多
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;

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

public class XpathDemo {

	public XpathDemo() throws FileNotFoundException, DocumentException {

		//testAddress();
		//testNodes();
		testNodes2();
	}

	private void testNodes() throws FileNotFoundException, DocumentException {
		
		Document doc = parse("product.xml");
		
		//String xpath = "/product/topics/topic[position() mod 2 =1]/name";
		String xpath = "/product/topics/topic[position() mod 2 =1]/name";
		
		List list = doc.selectNodes(xpath);
		for (Iterator i = list.iterator(); i.hasNext();) {
			Element element = (Element) i.next();
			System.out.println(element.getText());

		}
		
	}

	private void testNodes2() throws FileNotFoundException, DocumentException {
		
		Document doc = parse("product.xml");
		
		//String xpath = "/product/topics/topic[position() mod 2 =1]/name";
		String xpath = "/product/topics/topic[position() mod 2 =1]";
		
		List list = doc.selectNodes(xpath);
		for (Iterator i = list.iterator(); i.hasNext();) {
			Element element = (Element) i.next();
			System.out.println(element.valueOf("@id"));

		}
		
		xpath = "count(//topic)";
		
		System.out.println(doc.numberValueOf(xpath).intValue());
		
		String str = "TI";
		
		System.out.println(doc.valueOf("translate(/product/topics/topic[position()=1]/name, \"ti\", \""+str+"\")"));

	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			new  XpathDemo();
		} catch (FileNotFoundException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}
	
	public void testAddress() throws FileNotFoundException, DocumentException{
		Document doc = parse("address.xml");
		
		String xpath = "//last-name";
		Element element = (Element) doc.selectSingleNode(xpath);
		
		System.out.println(element.getText());
	}

	public Document parse(String xmlFile) throws FileNotFoundException, DocumentException{
		
		Document doc = null;
		
		SAXReader reader = new SAXReader();
		
		doc = reader.read(new FileInputStream(xmlFile));
		
		return doc;
		
	}

}

 

分享到:
评论

相关推荐

    xpath+helper

    google插件XPath Helper可以支持在网页点击元素生成xpath,整个抓取使用了xpath、正则表达式、消息中间件、多线程调度框架(参考)。xpath 是一种结构化网页元素选择器,支持列表和单节点数据获取,他的好处可以支持...

    XPath参考文档

    用于操作xml文档,对文档进行快速的节点读取操作

    python_spider:万维网

    功能:获取微博用户每一条微博的信息(包括转发,点赞的微博),其中原创微博抓取文字,配图,视频,发博时间,转发的微博则抓取转发的时间,转发的评价,财经杂志的链接,点赞的微博暂时实现(第一版)应用知识:1....

    PHP使用xpath解析XML的方法详解

    分享给大家供大家参考,具体如下...Xapth主要用来在XML文档中查询信息的工具,通过使用路径表达式可以解析XML文件,读取XML文件中的数据。 PHP解析XML推荐教程:PHP4和PHP5版本下解析XML文档的操作方法 需要PHP解析的X

    Python网络爬虫实例教程

    网络爬虫概述、 requests库入门、xapth语法详解、认识和应对反爬虫、模拟登录和验证码的处理、动态页面的分析方法、scrapy框架基础、 应对反爬虫策略、scrapy数据存储、提交数据和保持登录、crawlspider模板、图片...

Global site tag (gtag.js) - Google Analytics