【问题标题】:Getting value from a nested xml with attributes using minidom and etree使用 minidom 和 etree 从具有属性的嵌套 xml 中获取值
【发布时间】:2017-07-02 17:29:58
【问题描述】:

我有以下 xml 数据:

<response>
   <status>ok</status>
   <message>Done</message>
   <local-data>
      <name>
         <attribute key="first">John</attribute>
         <attribute key="middle">Maverick</attribute>
         <attribute key="last">Doe</attribute>
      <name>
   </local-data>
</response>

如何使用 minidom 和任何其他方法从 xml 中提取“first”或“middle”的值?

【问题讨论】:

    标签: python lxml minidom


    【解决方案1】:

    这段代码应该有帮助

    dom1 = parseString(xml_text)
    elements = dom1.getElementsByTagName("attribute")
    for element in elements:
        if element.getAttribute('key') == "first":
            print(element.firstChild.nodeValue)
    

    【讨论】:

      猜你喜欢
      • 2014-04-28
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      相关资源
      最近更新 更多