python对xpath的支持 - 转

 

 介绍python的Xpath的python开元项目:

1.libxml2-python-2.6.4.tar.gz

 

安装:下载对应python版本的软件包à解压àpython setup.py install

 

例一:
import libxml2
def text() :
doc = libxml2.parseFile('/tmp/books.xml')
for book in doc.xpathEval('/bookstore/book') :
        print book.content
doc.freeDoc()
 
例二
import libxml2
doc = libxml2.parseFile('/tmp/books.xml')
doc.xpathEval('/bookstore/book[1]/price + /bookstore/book[1]/price ')
doc.xpathEval('/bookstore/book[2]/price < 0')
doc.freeDoc()
 
例三
import libxml2
doc = libxml2.parseFile('/tmp/books.xml')
doc.xpathEval('child::book') #返回的结果是什么?为什么
doc.xpathEval('child::bookstore')
doc.xpathEval('descendant::bookstore')
doc.freeDoc()
 
例四: 
import libxml2
doc = libxml2.parseFile('/tmp/books.xml')
doc.xpathEval('/bookstore/book[price>35.00]')[0].content
doc.xpathEval('/bookstore/book[last()]')
doc.freeDoc()

 

2.Google的开源项目,python官方网站上推荐的xpath项目,版本为0.1

 

http://py-dom-xpath.googlecode.com/files/py-dom-xpath-0.1.tar.gz

 

安装:下载软件包à解压àpython setup.py install

 

例一
import xpath
import xml.dom.minidom
xml = xml.dom.minidom.parse('/tmp/books.xml')
doc = xml.documentElement
xpath.find('/bookstore/book[1]', doc)[0].toxml()
xpath.find('/bookstore', doc)
 
例二
import xpath
import xml.dom.minidom
xml = xml.dom.minidom.parse('/tmp/books.xml')
doc = xml.documentElement
context = xpath.XPathContext()
context.variables['max'] = 100
context.variables['min'] = 4
context.findvalues('//book[price>=$min and price<=$max]', doc)
 
还可以根据命名空间查询
详细参考文档:http://py-dom-xpath.googlecode.com/svn/trunk/doc/index.html
 

 

3.用python中自带的库解析xml

 

from xml.etree import ElementTree as XmlTree
xmlDoc = XmlTree.parse('/tmp/books.xml')
xmlRoot = xmlDoc.getroot()
value = xmlRoot.find('book/price').text
print value

本文出自 “深巷明朝卖杏花” 博客,请务必保留此出处http://padden.blog.51cto.com/2514314/464141

posted @ 2013-05-23 14:41  vivianC  阅读(796)  评论(0编辑  收藏  举报