python通过LXML库读取xml命名空间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python通过LXML库读取xml命名空间相关的知识,希望对你有一定的参考价值。
xml实例版本:
<a>
<city:table xmlns:city="city"> <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>
</a>
<city:table xmlns:city="city"></city:table>
声明命名空间,city为变量,xmlns定义city变量的值。
python 读取输出实例:
xp=xpxlm.xpath(‘//node()‘) print xp, #.items()#.tag for i in xp: if ‘‘ in i or ‘\n‘ in i: continue else: print i.tag
输出结果:
{city}table
heilongjiang
{city}haerbin
{city}daqing
guangdong
{city}guangzhou
{city}shenzhen
{city}zhuhai
........
改变<city:table xmlns:city="city">为
<city:table xmlns:city="a">
输出结果为:
{a}table
heilongjiang
{a}haerbin
{a}daqing
guangdong
{a}guangzhou
{a}shenzhen
{a}zhuhai
taiwan
{a}taibei
{a}gaoxiong
xinjiang
{a}wulumuqi
以上是关于python通过LXML库读取xml命名空间的主要内容,如果未能解决你的问题,请参考以下文章