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

 

 







posted @ 2017-09-09 13:38  LLSix  阅读(1283)  评论(0编辑  收藏  举报