2014-02-25 63 views
1

解析XML對於該XML與etree的Python

<locations> 

    <location> 
     <locationid>1</locationid> 
     <homeID>281</homeID> 
     <buildingType>Added</buildingType> 
     <address>A</address> 
     <address2>This is address2</address2> 
     <city>This is city/city> 
     <state>State here</state> 
     <zip>1234</zip> 
    </location> 
    <location> 
     <locationid>2</locationid> 
     <homeID>81</homeID> 
     <buildingType>Added</buildingType> 
     <address>B</address> 
     <address2>This is address2</address2> 
     <city>This is city/city> 
     <state>State here</state> 
     <zip>1234</zip> 
    </location> 
    . 
    . 
    . 
    . 
    <location> 
     <locationid>10</locationid> 
     <homeID>21</homeID> 
     <buildingType>Added</buildingType> 
     <address>Z</address> 
     <address2>This is address2</address2> 
     <city>This is city/city> 
     <state>State here</state> 
     <zip>1234</zip> 
    </location> 
</locations> 

我怎樣才能locationID的地址A,使用etree

這裏是我的代碼,

import urllib2 
import lxml.etree as ET 

url="url for the xml" 
xmldata = urllib2.urlopen(url).read() 
# print xmldata 
root = ET.fromstring(xmldata) 
for target in root.xpath('.//location/address[text()="A"]'): 
    print target.find('LocationID') 

獲取輸出None,哪些錯誤我在做什麼嗎?

+0

試試這個'.//location/[normalize-space(address)="A「]' – Naren

+0

@Naren謝謝,試過這個但是不行。 – fledgling

回答

2

首先,你的xml是不正確的。發佈時應該多加小心,並儘量避免讓其他用戶修復您的數據。

您可以搜索前面的兄弟,如:

import urllib2 
import lxml.etree as ET 

url="..." 
xmldata = urllib2.urlopen(url).read() 
root = ET.fromstring(xmldata) 
for target in root.xpath('.//location/address[text()="A"]'):                         
    for location in [e for e in target.itersiblings(preceding=True) if e.tag == "locationid"]:                 
     print location.text 

或者自己直接從xpath表達,如:

import urllib2 
import lxml.etree as ET 

url="..." 
xmldata = urllib2.urlopen(url).read() 
root = ET.fromstring(xmldata) 
print root.xpath('.//location/address[text()="A"]/preceding-sibling::locationid/text()')[0] 

運行其中一方想:

python2 script.py 

產量:

1 
+1

對不起,我的XML有一些錯誤。下次我會照顧它。謝謝 – fledgling