码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-09 14:32:57      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:XML   span   定义   lxml   print   aqi   log   logs   命名空间   

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命名空间

标签:XML   span   定义   lxml   print   aqi   log   logs   命名空间   

原文地址:http://www.cnblogs.com/liuliu-word/p/7498050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!