xml实例:

版本一:

<?xml version="1.0" encoding="UTF-8"?><country name="chain"><provinces><heilongjiang name="citys"><haerbin/><daqing/></heilongjiang><guangdong name="citys"><guangzhou/><shenzhen/><huhai/></guangdong><taiwan name="citys"><taibei/><gaoxiong/></taiwan><xinjiang name="citys"><wulumuqi waith="tianqi">晴</wulumuqi></xinjiang></provinces></country>

没有空格,换行,的版本

python操作操作实例:

from lxml import etree
class r_xpath_xml(object):
def __init__(self):
self.xmetrpa=etree.parse('info.xml') #读取xml数据
pass
def xpxm(self):
xpxlm=self.xmetrpa
print etree.tostring(xpxlm) #打印xml数据
root=xpxlm.getroot() #获得该树的树根
print root.tag,' ', #打印根标签名
print root.items() #获得标签属性名称和属性值
for a in root: ##遍历根下一集级标签
print a.tag,a.items(),a.text,' 被打印的类型为: ',type(a) #打印标签名称,标签属性,标签数据
for b in a:
print b.tag,b.items(),b.text#,b
for c in b:
print c.tag,c.items(),c.text#,c
for d in c:
print d.tag,d.items(),d.test,d
print xpxlm.xpath('//node()')#.items()#.tag
print '====================================================================================================='
xa=xpxlm.xpath('//heilongjiang/*')
print xa
for xb in xa:
print xb.tag,xb.items(),xb.text
xc=xpxlm.xpath('//xinjiang/*')
print xc
for xd in xc:
print xd.tag,xd.items(),xd.text
if __name__ == '__main__':
xpx=r_xpath_xml()
xpx.xpxm()
应用for循环遍历标签层次结构,tag获取标签名,items()通过字典函数获取[('属性名' , '属性值')],text获取标签对之间的数据。tag,items(),text针对的类型为:<type 'lxml.etree._Element'>
打印结果:
<country name="chain"><provinces><heilongjiang name="citys"><haerbin/><daqing/></heilongjiang><guangdong name="citys"><guangzhou/><shenzhen/><huhai/></guangdong><taiwan name="citys"><taibei/><gaoxiong/></taiwan><xinjiang name="citys"><wulumuqi waith="tianqi">晴</wulumuqi></xinjiang></provinces></country>
country [('name', 'chain')]
provinces [] None 被打印的类型为: <type 'lxml.etree._Element'>
heilongjiang [('name', 'citys')] None
haerbin [] None
daqing [] None
guangdong [('name', 'citys')] None
guangzhou [] None
shenzhen [] None
huhai [] None
taiwan [('name', 'citys')] None
taibei [] None
gaoxiong [] None
xinjiang [('name', 'citys')] None
wulumuqi [('waith', 'tianqi')] 晴
[<Element country at 0x2d47b20>, <Element provinces at 0x2d47990>, <Element heilongjiang at 0x2d479b8>, <Element haerbin at 0x2d47558>, <Element daqing at 0x2d47328>, <Element guangdong at 0x2d47300>, <Element guangzhou at 0x2d476e8>, <Element shenzhen at 0x2d47530>, <Element huhai at 0x2d472d8>, <Element taiwan at 0x2d47260>, <Element taibei at 0x2d47238>, <Element gaoxiong at 0x2d47080>, <Element xinjiang at 0x2d47710>, <Element wulumuqi at 0x2d47968>, u'\u6674']
=====================================================================================================
[<Element haerbin at 0x2d479b8>, <Element daqing at 0x2d47148>]
haerbin [] None
daqing [] None
[<Element wulumuqi at 0x2d47968>] 类型为: <type 'list'>
wulumuqi [('waith', 'tianqi')] 晴

xml实例:

版本二:

<?xml version="1.0" encoding="UTF-8"?>
<country name="chain">
<provinces>
<city:table xmlns:city="http://www.w3school.com.cn/furniture">
<heilongjiang name="citys"><city:haerbin/><city:daqing/></heilongjiang>
<guangdong name="citys"><city:guangzhou/><city:shenzhen/><city:zhuhai/></guangdong>
<taiwan name="citys"><city:taibei/><city:gaoxiong/></taiwan>
<xinjiang name="citys"><city:wulumuqi>晴</city:wulumuqi></xinjiang>
</city:table>
</provinces>
</country>
实例:
print xpxlm.xpath('//node()')
打印结果:
空格回车字符,命名空间。
[<Element country at 0x2e79b20>, '\n    ', <Element provinces at 0x2e79990>, '\n        ', <Element {http://www.w3school.com.cn/furniture}table at 0x2e79710>, '\n        ', <Element heilongjiang at 0x2e799b8>, <Element {http://www.w3school.com.cn/furniture}haerbin at 0x2e79328>, <Element {http://www.w3school.com.cn/furniture}daqing at 0x2e79968>, '\n        ', <Element guangdong at 0x2e79530>, <Element {http://www.w3school.com.cn/furniture}guangzhou at 0x2e79300>, <Element {http://www.w3school.com.cn/furniture}shenzhen at 0x2e792d8>, <Element {http://www.w3school.com.cn/furniture}zhuhai at 0x2e79260>, '\n        ', <Element taiwan at 0x2e79238>, <Element {http://www.w3school.com.cn/furniture}taibei at 0x2e79080>, <Element {http://www.w3school.com.cn/furniture}gaoxiong at 0x2e79058>, '\n        ', <Element xinjiang at 0x2e796e8>, <Element {http://www.w3school.com.cn/furniture}wulumuqi at 0x2e79558>, u'\u6674', '\n        ', '    \n    ', '\n']

去掉空格:

        xp=xpxlm.xpath('//node()')
print xp, #.items()#.tag
for i in xp:
if '' in i or '\n' in i:
continue
else:
print i.tag

通过判断去除空格换行符号

输出结果:

provinces
{city}table
heilongjiang
{city}haerbin
{city}daqing
guangdong
{city}guangzhou
{city}shenzhen
{city}zhuhai
taiwan
{city}taibei
{city}gaoxiong
xinjiang
{city}wulumuqi
												

Python通过lxml库遍历xml通过xpath查询(标签,属性名称,属性值,标签对属性)的更多相关文章

  1. python通过LXML库读取xml命名空间

    xml实例版本: <a> <city:table xmlns:city="city"> <heilongjiang name="citys& ...

  2. js遍历对象所有的属性名称和值

    /* * 用来遍历指定对象所有的属性名称和值 * obj 需要遍历的对象 * author: Jet Mah * website: http://www.javatang.com/archives/2 ...

  3. 遍历JavaScript某个对象所有的属性名称和值

    /* * 用来遍历指定对象所有的属性名称和值 * obj 需要遍历的对象 */ function allPrpos(obj) { // 用来保存所有的属性名称和值 var props = " ...

  4. python lxml库生成xml文件-节点命名空间问题

    lxml库,处理xml很强大,官方文档:https://lxml.de/tutorial.html#namespaces 例如: 我们要生成如下格式的报文: <ttt:jesson xmlns: ...

  5. C#使用Linq to XML进行XPath查询

    最近在用到HtmlAgliltyPack进行结点查询时,发现这里选择结点使用的是XPath.所以这里总结一下在C#中使用XPath查询XML的方法.习惯了用Linq,这里也是用的Linq to xml ...

  6. 【xml】python的lxml库使用

    1.官方教程:http://lxml.de/tutorial.html#parsing-from-strings-and-files  最重要的文档,看完基本就能用了 2.lxml支持xpath,xp ...

  7. Python 使用PyMySql 库 连接MySql数据库时 查询中文遇到的乱码问题(实测可行) python 连接 MySql 中文乱码 pymysql库

    最近所写的代码中需要用到python去连接MySql数据库,因为是用PyQt5来构建的GUI,原本打算使用PyQt5中的数据库连接方法,后来虽然能够正确连接上发现还是不能提交修改内容,最后在qq交流群 ...

  8. Python中使用面状矢量裁剪栅格影像,并依据Value值更改矢量属性

    本文整体思路:在Python中使用Geopandas库,依次读取shp文件的每一个面状要素,获取其空间边界信息并裁剪对应的栅格影像,计算所裁剪影像Value值的众数,将其设置为对应面状要素的NewTY ...

  9. Python 爬虫 解析库的使用 --- XPath

    一.使用XPath XPath ,全称XML Path Language,即XML路径语言,它是一门在XML文档中查找信息的语言.它最初是用来搜寻XML文档的,但是它同样适用于HTML文档的搜索. 所 ...

随机推荐

  1. 【Python开发】PyInstaller打包Python程序

    PyInstaller是一个能将Python程序转换成单个可执行文件的程序, 操作系统支持Windows, Linux, Mac OS X, Solaris和AIX.并且很多包都支持开箱即用,不依赖环 ...

  2. springboot实战日记(一)数据库基本信息

    摘要:基于spring boot的后端实现,开发一个微信小程序点餐系统,主要是写写思路和遇到的问题以及分享读到的好文章. 项目分析: 1.角色划分,就是开有什么人使用这个系统,买家(手机端),卖家(p ...

  3. Burp Suite 如何抓取HTTPS请求

    1,下载安装burp suite工具 https://portswigger.net/burp/communitydownload 如果是windows系统,选择windows点击Download下载 ...

  4. jQuery实现简单导航栏的样式切换

    style css样式部分: ul{ margin: 0 auto; height: 50px; background-color: #369;} ul>li{ text-decoration: ...

  5. 40 多线程(十二)——ReentrantLock 可重入锁

    我们使用的synchronized加的锁是可以延续使用的,如下: public void test() { //第一次获得锁 synchronized(this) { while(true) { // ...

  6. kubernetes 实践二:kubectl命令使用

    这里记录kubernetes学习和使用过程中的内容. CentOS7 k8s-1.13 flanneld-0.10 docker-18.06 etcd-3.3 kubectl用法概述 kubectl是 ...

  7. docker(四):集群swarm

    docker使用入门(四):集群swarm swarm是一组位于同一集群且运行docker的机器,用户可以通过swarm manager向swarm输入命令,swarm中的机器可以是虚拟机也可以是物理 ...

  8. AS3.0 字母大小写转换

    字母大小写转换: /** * * *-------------------------* * | *** 字母大小写转换 *** | * *-------------------------* * * ...

  9. jwt 0.9.0(二)jwt官网资料总结

    1.JWT描述 Jwt token由Header.Payload.Signature三部分组成,这三部分之间以小数点”.”连接,JWT token长这样: eyJhbGciOiJIUzI1NiIsIn ...

  10. Kafka 初识

    1.Kafka 是什么? 用一句话概括一下:Apache Kafka 是一款开源的消息引擎系统. 倘若“消息引擎系统“这个词对你来说有点陌生的话,那么“消息队列“.“消息中间件”的提法想必你一定是有所 ...