Python通过lxml库遍历xml通过xpath查询(标签,属性名称,属性值,标签对属性)
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查询(标签,属性名称,属性值,标签对属性)的更多相关文章
- python通过LXML库读取xml命名空间
xml实例版本: <a> <city:table xmlns:city="city"> <heilongjiang name="citys& ...
- js遍历对象所有的属性名称和值
/* * 用来遍历指定对象所有的属性名称和值 * obj 需要遍历的对象 * author: Jet Mah * website: http://www.javatang.com/archives/2 ...
- 遍历JavaScript某个对象所有的属性名称和值
/* * 用来遍历指定对象所有的属性名称和值 * obj 需要遍历的对象 */ function allPrpos(obj) { // 用来保存所有的属性名称和值 var props = " ...
- python lxml库生成xml文件-节点命名空间问题
lxml库,处理xml很强大,官方文档:https://lxml.de/tutorial.html#namespaces 例如: 我们要生成如下格式的报文: <ttt:jesson xmlns: ...
- C#使用Linq to XML进行XPath查询
最近在用到HtmlAgliltyPack进行结点查询时,发现这里选择结点使用的是XPath.所以这里总结一下在C#中使用XPath查询XML的方法.习惯了用Linq,这里也是用的Linq to xml ...
- 【xml】python的lxml库使用
1.官方教程:http://lxml.de/tutorial.html#parsing-from-strings-and-files 最重要的文档,看完基本就能用了 2.lxml支持xpath,xp ...
- Python 使用PyMySql 库 连接MySql数据库时 查询中文遇到的乱码问题(实测可行) python 连接 MySql 中文乱码 pymysql库
最近所写的代码中需要用到python去连接MySql数据库,因为是用PyQt5来构建的GUI,原本打算使用PyQt5中的数据库连接方法,后来虽然能够正确连接上发现还是不能提交修改内容,最后在qq交流群 ...
- Python中使用面状矢量裁剪栅格影像,并依据Value值更改矢量属性
本文整体思路:在Python中使用Geopandas库,依次读取shp文件的每一个面状要素,获取其空间边界信息并裁剪对应的栅格影像,计算所裁剪影像Value值的众数,将其设置为对应面状要素的NewTY ...
- Python 爬虫 解析库的使用 --- XPath
一.使用XPath XPath ,全称XML Path Language,即XML路径语言,它是一门在XML文档中查找信息的语言.它最初是用来搜寻XML文档的,但是它同样适用于HTML文档的搜索. 所 ...
随机推荐
- java 多线程 面试
1.多线程有什么用? (1)发挥多核CPU的优势: 当前,应用服务器至少也都是双核的,4核.8核甚至16核的也都不少见,如果是单线程的程序,那么在双核CPU上就浪费了50%,在4核CPU上就浪费了75 ...
- 机器学习技法总结(一):支持向量机(linear support vector machine,dual support vector machine)
第一阶段技法: large margin (the relationship between large marin and regularization), hard-SVM,soft-SVM,du ...
- Windows 下使用 Composer 安装 thinkphp
我用 XAMPP 安装 thinkphp 会出错,所以把环境换成了 phpStudy,这样甚至不用到处找安装包,直接去官网有最新版本,PHP 版本也是比较新的. 安装 phpStudy 先去官网下载安 ...
- mysql 按照计算值排序
SELECT title,browse_num/exposure_num as click_rate FROM `tf_news` ORDER BY browse_num/exposure_num d ...
- Google深度学习开源框架TenseorFlow安装
Google近期发布了TensorFlow,考录到Google出品,必属精品,估计这玩意会火,不过火钳刘明已经来不及了 今天才想着安装来试试 TensorFlow官网:https://www.tens ...
- vm采用NAT方式连接时,设置静态ip
一,共享无线连接或本地连接,给VMnet8. 在网络配置中,选着无线连接,右键属性,共享. 这里默认给虚拟网卡VMnet8,分配了IP:192.168.137.1. 二,在VMware中配置VMnet ...
- Fedora30 install VS Code
We currently ship the stable 64-bit VS Code in a yum repository, the following script will install t ...
- Python知识点图片
- A Simple Question of Chemistry
#include<stdio.h> int main() { int i, l; ]; ]; l = ; ) { l++; } ; a[i]!= && i<l; i+ ...
- SAS学习笔记63 如何导出Log
如上,将Log输出,然后又恢复到SAS系统里面的Log,把需要运行的程序放到他们中间就可以了.这种方法不会出现Log打印满了的情况 这种是先输出在SAS系统里面,然后在输出,在SAS里面Log的行是有 ...