【问题标题】:Parsing xml with etree Python使用 etree Python 解析 xml
【发布时间】:2014-03-27 06:17:31
【问题描述】:

对于这个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,我在这里做错了什么?

【问题讨论】:

  • 试试这个'.//location/[normalize-space(address)="A"]'
  • @Naren 谢谢,试过了但没用。

标签: python xml xml-parsing elementtree xml.etree


【解决方案1】:

首先,您的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

【讨论】:

  • 对不起,我的 xml 有一些错误。下次我会照顾它。谢谢
猜你喜欢
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 2012-02-01
  • 2011-10-29
  • 2017-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多