在lxml中查找具有函数find的元素

时间:2015-09-22 16:35:06

标签: python xml lxml

我有下一个xml:

<?xml version='1.0' encoding='utf-8'?>
<SOAP:Envelope xmlns:SOAP="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing">
  <SOAP:Header>
  </SOAP:Header>
  <SOAP:Body>
    <Server_Reply xmlns="some_url">
      <conversionRate>
        <conversionRateDetail>
          <currency>dollar</currency>
        </conversionRateDetail>
      </conversionRate>
    </Server_Reply>
  </SOAP:Body>
</SOAP:Envelope>

它在reply.txt。然后我做:

with open('reply.txt', 'r') as f:
        reply = f.read()

reply_element = fromstring(reply)

我需要找到Server_Reply元素。 当我这样做时:

response = reply_element.find('Body/Server_Reply')

但它返回None。 怎么做对吗?最后,我需要获取Server_Reply元素。

3 个答案:

答案 0 :(得分:0)

您需要使用.//来表示您要查找当前元素(Body)的后代(不是直接子代)的SOAP:Envelope

此外,由于您的xml使用名称空间,您必须在xpath中包含名称空间(您将.find()包含在内。示例 -

response = reply_xml.find('.//{http://www.w3.org/2003/05/soap-envelope}Body/{some_url}Server_Reply')

或者

response = reply_xml.find('.//SOAP:Body/dummy:Server_Reply',namespaces = {'SOAP':'http://www.w3.org/2003/05/soap-envelope', 'dummy':'some_url'})

演示 -

In [55]: s = """<SOAP:Envelope xmlns:SOAP="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing">
   ....:   <SOAP:Header>
   ....:   </SOAP:Header>
   ....:   <SOAP:Body>
   ....:     <Server_Reply xmlns="some_url">
   ....:       <conversionRate>
   ....:         <conversionRateDetail>
   ....:           <currency>dollar</currency>
   ....:         </conversionRateDetail>
   ....:       </conversionRate>
   ....:     </Server_Reply>
   ....:   </SOAP:Body>
   ....: </SOAP:Envelope>"""

In [56]: reply_xml = etree.fromstring(s)

In [57]: reply_xml.find('.//SOAP:Body/dummy:Server_Reply',namespaces = {'SOAP':'http://www.w3.org/2003/05/soap-envelope', 'dummy':'some_url'})
Out[57]: <Element {some_url}Server_Reply at 0x481d708>

In [58]: reply_xml.find('.//{http://www.w3.org/2003/05/soap-envelope}Body/{some_url}Server_Reply')
Out[58]: <Element {some_url}Server_Reply at 0x481d708>

答案 1 :(得分:0)

我发现xpath更加直观和简单:

from lxml import etree

xml = """<?xml version='1.0' encoding='utf-8'?>
<SOAP:Envelope xmlns:SOAP="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://www.w3.org/2005/08/addressing">
  <SOAP:Header>
  </SOAP:Header>
  <SOAP:Body>
    <Server_Reply xmlns="some_url">
      <conversionRate>
        <conversionRateDetail>
          <currency>dollar</currency>
        </conversionRateDetail>
      </conversionRate>
    </Server_Reply>
  </SOAP:Body>
</SOAP:Envelope>"""

et = etree.fromstring(xml)
server_reply = et.xpath('//*[local-name()="Server_Reply"]')

答案 2 :(得分:-1)

使用xml.etree执行此操作。

#!/usr/bin/env python
import sys
from xml.etree import ElementTree
from lxml import etree

def run(fileName):
    parser = etree.XMLParser(ns_clean=True)
    data = ElementTree.parse(fileName, parser).getroot()
    namespaces = data.nsmap
    namespaces['some_url'] = 'some_url'
    # Creating without duplicates here, which contains the unique list of elements determined by values of subelements
    for row in data.findall('.//SOAP:Body/some_url:Server_Reply', namespaces = namespaces):
        print row

if __name__ == "__main__":
    run(sys.argv[1])

然后使用XML文件作为参数运行python:

python findElement.py sampleFile.xml