تحليل ملف XML باستخدام etree Python
-
21-12-2019 - |
سؤال
لهذا XML
<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
، ما الخطأ الذي أفعله هنا؟
المحلول
أولا وقبل كل شيء، الخاص بك 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
لا تنتمي إلى StackOverflow