查找xml cdata 节点并去掉 CDATA 节点标记

发布时间 2023-05-26 16:05:36作者: vx_guanchaoguo0
内容如下
<?xml version="1.0" encoding="UTF-8"?>
<transstatus>
    <transname>C-QXZSPJ-QXZSPJ-015-1</transname>
    <id>2ca8df47-3b2d-4d8f-8b70-be68448dd97d</id>
    <status_desc>Finished</status_desc>
    <error_desc/>
    <log_date>2023/05/26 14:36:13.127</log_date>
    <paused>N</paused>
    <stepstatuslist>
        <stepstatus>
            <stepname>tableInputStep</stepname>
            <copy>0</copy>
            <linesRead>0</linesRead>
            <linesWritten>0</linesWritten>
            <linesInput>0</linesInput>
            <linesOutput>0</linesOutput>
            <linesUpdated>0</linesUpdated>
            <linesRejected>0</linesRejected>
            <errors>0</errors>
            <statusDescription>已完成</statusDescription>
            <seconds>0.0</seconds>
            <speed>0</speed>
            <priority>-</priority>
            <stopped>N</stopped>
            <paused>N</paused>
        </stepstatus>
        <stepstatus>
            <stepname>insertUpdateStep</stepname>
            <copy>0</copy>
            <linesRead>0</linesRead>
            <linesWritten>0</linesWritten>
            <linesInput>0</linesInput>
            <linesOutput>0</linesOutput>
            <linesUpdated>0</linesUpdated>
            <linesRejected>0</linesRejected>
            <errors>0</errors>
            <statusDescription>已完成</statusDescription>
            <seconds>0.0</seconds>
            <speed>0</speed>
            <priority>-</priority>
            <stopped>N</stopped>
            <paused>N</paused>
        </stepstatus>
        <stepstatus>
            <stepname>stepsMetricsStep</stepname>
            <copy>0</copy>
            <linesRead>1</linesRead>
            <linesWritten>1</linesWritten>
            <linesInput>0</linesInput>
            <linesOutput>0</linesOutput>
            <linesUpdated>0</linesUpdated>
            <linesRejected>0</linesRejected>
            <errors>0</errors>
            <statusDescription>已完成</statusDescription>
            <seconds>0.2</seconds>
            <speed>5</speed>
            <priority>-</priority>
            <stopped>N</stopped>
            <paused>N</paused>
        </stepstatus>
        <stepstatus>
            <stepname>setVariableStep</stepname>
            <copy>0</copy>
            <linesRead>1</linesRead>
            <linesWritten>1</linesWritten>
            <linesInput>0</linesInput>
            <linesOutput>0</linesOutput>
            <linesUpdated>0</linesUpdated>
            <linesRejected>0</linesRejected>
            <errors>0</errors>
            <statusDescription>已完成</statusDescription>
            <seconds>0.2</seconds>
            <speed>5</speed>
            <priority>-</priority>
            <stopped>N</stopped>
            <paused>N</paused>
        </stepstatus>
    </stepstatuslist>
    <first_log_line_nr>0</first_log_line_nr>
    <last_log_line_nr>2029</last_log_line_nr>
    <result>
        <lines_input>0</lines_input>
        <lines_output>0</lines_output>
        <lines_read>0</lines_read>
        <lines_written>0</lines_written>
        <lines_updated>0</lines_updated>
        <lines_rejected>0</lines_rejected>
        <lines_deleted>0</lines_deleted>
        <nr_errors>0</nr_errors>
        <nr_files_retrieved>0</nr_files_retrieved>
        <entry_nr>0</entry_nr>
        <result>Y</result>
        <exit_status>0</exit_status>
        <is_stopped>N</is_stopped>
        <log_channel_id>fb424211-b0f7-45d1-9fd5-be55785e0c58</log_channel_id>
        <log_text/>
    </result>
    <logging_string>&lt;![CDATA[H4sIAAAAAAAAAK2TW0sCQRTH3/sU57HAy+56eRB8EN1iJS/tJaOQ2NapBnTWdkajt4KQoAJfe6wMX6Jnkfo2avYt2qFaLFxI2XmYYS7n9/9zzhxJkGJRIRGVkiDGU7FkSoxBGLLhrZ1drZz/WQQxERbd89FgOBp2pm/Pk9uHaf/x4643vX8aDS7Gr+fj/jXA3tzA6oo0VwUTihxmNGsmQxpDzYjApW1CkMVQDZgN7o15YFLkA97X1Y0qrFp2o4FZWhLcseaj5XLqSCHNFvOU1jHB9NgVcpBZw+QITlrIOQuBVbcp31pfTrBNIj5Q6qJoATEHW9TDjl9uJlfdce/yvduBVSUthKDEJzUthqDCJ4Nv5bSvV4rYtulg7tijaogxbgqRNnZs0kCEQfv7EY1EfB3OR3mhoOlyuZgpyDzbbbPeclP9tyx+5fsXW8nNkHV+XGoxtwpLQ5XiplKUi0ZhhissTSsZepA4Vc7kguQZ5VxGl4MkVlQlWKAq5+VsoEnMljRdV359yfgiMK+vzUOGHBDBsU/pIg2ySAd/Ao7BUkNABQAA]]&gt;</logging_string>
</transstatus>

获取 cdata

 with open(file_name, 'r') as f:
        xml_string = f.read()
    import xml.etree.ElementTree as ET
    root = ET.fromstring(xml_string)
    for c_data  in root.findall(".//logging_string"):
        data_content =  c_data.text.replace("<![CDATA[", "").replace("]]>", "")
        print(data_content)