【问题标题】:iterate through XML?遍历 XML?
【发布时间】:2023-04-06 04:54:01
【问题描述】:

用 python 浏览 XML 最简单的方法是什么?

<html>
 <body>
  <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:body>
    <getservicebyidresponse xmlns="http://www.something.com/soa/2.0/SRIManagement">
     <code xmlns="">
      0
     </code>
     <client xmlns="">
      <action xsi:nil="true">
      </action>
      <actionmode xsi:nil="true">
      </actionmode>
      <clientid>
       405965216
      </clientid>
      <firstname xsi:nil="true">
      </firstname>
      <id xsi:nil="true">
      </id>
      <lastname>
       Last Name
      </lastname>
      <role xsi:nil="true">
      </role>
      <state xsi:nil="true">
      </state>
     </client>
    </getservicebyidresponse>
   </soapenv:body>
  </soapenv:envelope>
 </body>
</html>

我会使用正则表达式并尝试获取我需要的行的值,但是有没有 python 的方式? xml[0][1] 之类的?

【问题讨论】:

标签: python xml python-2.6


【解决方案1】:

正如@deceze 已经指出的那样,您可以在此处使用xml.etree.ElementTree

import xml.etree.ElementTree as ET
tree = ET.parse("path_to_xml_file")
root = tree.getroot()

你可以遍历root的所有子节点:

for child in root.iter():
    if child.tag == 'clientid':
        print(child.tag, child.text.strip())

子节点是嵌套的,我们可以通过索引访问特定的子节点,所以root[0][1]应该可以工作(只要索引正确)。

【讨论】:

  • 这给了我IOError: [Errno 36] File name too long:
  • 请参考这个答案 - stackoverflow.com/a/29891397/2689986。您可能会传递文件内容而不是将文件名传递给 ET.parse 函数。
  • 好的,root = ET.fromstring(xml) 解决了这个问题,我没有 XML 文件
  • for child in root: print(child.tag, child.attrib) 给我('body', {})
  • 对于 RHEL 库存配置 (Python 2.6.6) 它的:root.getiterator(): 现在它可以工作了,我明白我需要做什么谢谢
猜你喜欢
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 2010-11-04
  • 2021-10-11
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
相关资源
最近更新 更多