Python 将 XML ElementTree findall 的结果写入文件

Python write result of XML ElementTree findall to a file

我想编写 python 代码以从源 XML 文件中提取一些数据并写入新文件。我的源文件是这样的:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

    <soapenv:Header/>
    <soapenv:Body>
        <SessionID xmlns="http://www.niku.com/xog">12345</SessionID>
        <QueryResult xmlns="http://www.niku.com/xog/Query" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <Records>
                <Record>
                  <id>1</id>
                  <date_start>2020-10-04T00:00:00</date_start>
                  <date_end>2020-10-10T00:00:00</date_end>
                  <name>Payne, Max</name>
                </Record>
                <Record>
                  <id>2</id>
                  <date_start>2020-10-04T00:00:00</date_start>
                  <date_end>2020-10-10T00:00:00</date_end>
                  <name>Reno, Jean</name>
                </Record>
            </Records>
        </QueryResult>
    </soapenv:Body>
</soapenv:Envelope>

我想将以下输出写入新的 xml 文件。

<Records>
    <Record>
      <id>1</id>
      <date_start>2020-10-04T00:00:00</date_start>
      <date_end>2020-10-10T00:00:00</date_end>
      <name>Payne, Max</name>
    </Record>
    <Record>
      <id>2</id>
      <date_start>2020-10-04T00:00:00</date_start>
      <date_end>2020-10-10T00:00:00</date_end>
      <name>Reno, Jean</name>
    </Record>
</Records>

我能够从此代码中获得以下结果。

import xml.etree.ElementTree as ET

tree = ET.parse('my_file.xml')

root = tree.getroot()

for xtag in root.findall('.//{http://www.niku.com/xog/Query}Record'):
    print(xtag)

结果:

<Element '{http://www.niku.com/xog/Query}Record' at 0x00000216BA69B778>
<Element '{http://www.niku.com/xog/Query}Record' at 0x00000216BA6A3228>

谁能帮我完成要求?

在您的例子中,print(xtag) 打印 xtag 对象而不是字符串。为此,您需要使用树的 tostring() 方法将对象转换为字符串。此外,您似乎希望获得整个 <Records> 块而不是单个 <Record> 元素;为此你不需要循环。

import xml.etree.ElementTree as ET

tree = ET.parse('test.xml')
root = tree.getroot()

records = root.find('.//{http://www.niku.com/xog/Query}Records')
print(ET.tostring(records).decode("utf-8"))

输出

<ns0:Records xmlns:ns0="http://www.niku.com/xog/Query">
                <ns0:Record>
                  <ns0:id>1</ns0:id>
                  <ns0:date_start>2020-10-04T00:00:00</ns0:date_start>
                  <ns0:date_end>2020-10-10T00:00:00</ns0:date_end>
                  <ns0:name>Payne, Max</ns0:name>
                </ns0:Record>
                <ns0:Record>
                  <ns0:id>2</ns0:id>
                  <ns0:date_start>2020-10-04T00:00:00</ns0:date_start>
                  <ns0:date_end>2020-10-10T00:00:00</ns0:date_end>
                  <ns0:name>Reno, Jean</ns0:name>
                </ns0:Record>
            </ns0:Records>

您也可以使用 lxml 模块,它的输出略有不同。

from lxml import etree

tree = etree.parse('test.xml')
root = tree.getroot()

records = root.find('.//{http://www.niku.com/xog/Query}Records')
print(etree.tostring(records).decode("utf-8"))

输出

<Records xmlns="http://www.niku.com/xog/Query" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                <Record>
                  <id>1</id>
                  <date_start>2020-10-04T00:00:00</date_start>
                  <date_end>2020-10-10T00:00:00</date_end>
                  <name>Payne, Max</name>
                </Record>
                <Record>
                  <id>2</id>
                  <date_start>2020-10-04T00:00:00</date_start>
                  <date_end>2020-10-10T00:00:00</date_end>
                  <name>Reno, Jean</name>
                </Record>
            </Records>