1. 程式人生 > >python通過LXML庫讀取xml命名空間

python通過LXML庫讀取xml命名空間

con family python continue 聲明 heilon style color ont

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