如何使用命名空间从 xml 文件中提取值?

How to extract values from xml file with namespaces?

我有下面显示的 xml 文件,它有命名空间,我正在尝试为它提取 Node24

的值

我当前的代码如下,没有打印任何东西:

import xml.etree.ElementTree as ET

filename = 'ifile.xml'
tree = ET.parse(filename)
root = tree.getroot()

for neighbor in root.iter('Node24'):
    print(neighbor)

我的预期输出是:

03-c34ko
04-c64ko
07-c54ko  

就是ifile.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<data-main-43:DATAMAINXZ123 xmlns="https://example.com/DATA-MAIN-XZ123" xmlns:data-gen="https://example.com/DATA-GEN" xmlns:data-main-43="https://example.com/DATA-MAIN-XZ123" xmlns:xsi="http://www.w3.org/2011/XMLSchema-instance" xsi:schemaLocation="https://example.com/DATA-MAIN-XZ123 data-main-ir21-12.1.xsd">
  <MAINXZ123FileHeader>
    <DATAGenSchemaVersion>2.4</DATAGenSchemaVersion>
    <DATAMAINXZ123SchemaVersion>12.1</DATAMAINXZ123SchemaVersion>
  </MAINXZ123FileHeader>
  <Node1>
    <Node2>WTRT DDK</Node2>
    <Node3>XYZW</Node3>
    <Node4>
      <Node5>
        <Node6>XYZW882</Node6>
        <Node5Type>Ter</Node5Type>
        <Node5Data>
          <Node9>
            <Node10>
              <Node11>2019-02-18</Node11>
              <Node12>
                <Node13>
                  <Node14>
                    <Node15>Ermso</Node15>
                    <Node16>
                      <PrimaryNode16>
                        <Node18>19.32</Node18>
                        <Node18>12.11</Node18>
                      </PrimaryNode16>
                      <SecondaryNode16>
                        <Node18>82.97</Node18>
                        <Node18>12.41</Node18>
                      </SecondaryNode16>
                    </Node16>
                    <Node20>Muuatippw</Node20>
                  </Node14>
                </Node13>
              </Node12>
              <Node21>
                <Node22>
                  <Node23>
                    <Node24>03-c34ko</Node24>
                    <Node24>04-c64ko</Node24>
                    <Node24>07-c54ko</Node24>
                  </Node23>
                  <Node26Node22EdgeAgent>
                    <Node26>jjkksonem</Node26>
                    <PrimaryNode18DEANode26>
                      <Node18>2.40</Node18>
                    </PrimaryNode18DEANode26>
                  </Node26Node22EdgeAgent>
                </Node22>
              </Node21>
              <Node28>
                <Node29>
                  <Node30>false</Node30>
                  <Node31>true</Node31>
                </Node29>
              </Node28>
            </Node10>
          </Node9>
        </Node5Data>
      </Node5>
    </Node4>
  </Node1>
</data-main-43:DATAMAINXZ123>

我该怎么做?提前致谢。

我使用的是正则表达式,所以这是备选答案。 我将 xml 转换为字符串,然后搜索 Node24

之间的所有字符串
import xml.etree.ElementTree as ET
import re

filename = 'ifile.xml'
tree = ET.parse(filename)
root = tree.getroot()
xml_str = ET.tostring(root) 
for s in re.findall(r'ns0:Node24>(.*?)</ns0:Node24', str(xml_str)):
    print(s)

结果:

03-c34ko
04-c64ko
07-c54ko

一样,只需将命名空间uri添加到元素名称...

import xml.etree.ElementTree as ET

filename = 'ifile.xml'
tree = ET.parse(filename)
root = tree.getroot()

for neighbor in root.iter('{https://example.com/DATA-MAIN-XZ123}Node24'):
    print(neighbor.text)

注意:我还添加了 .textneighbor 以便您获得请求的结果。