python XML parse error: 'ParseError: unbound prefix:' (Can't find any issue with xml file)

python XML parse error: 'ParseError: unbound prefix:' (Can't find any issue with xml file)

您好,我在解析以下 xml 文件时遇到以下错误(找不到 xml 文件的任何问题)。我希望这个 xml 通过解析而不失败。 我正在使用 xml.etree 进行解析,我在 etree 中使用 formstring 方法。 我正在使用:

import xml.etree.ElementTree as ET
root_payload = ET.fromstring(xml_file)

请帮忙。

<!--
TRACE CONFIGS EXAMPLE OF TURN ON DEBUG TRACING LEVELS

Global Variables to be replaced in this document:
$pmaa_ip_addr$ - Controlling PMAA IP Address
$auth$ - Authorization key for the PMAA interface
$dpu_ident$ - DPU unique identifier

Command: https://$pmaa_ip_addr$:8443/dpudatastore/id=PMA-$dpu_ident$/restconf/data/adtran-subsystem-traces:traces
Request: PUT
Authorization: Basic $auth$
Accept: application/xrd+xml
Content-Type: application/xrd+xml-->
<adtn-subs-trace:traces xmlms:adtn-subs-trace="http://www.adtran.com/ns/yang/adtran-subsystem-traces">
<adtn-subs-trace:trace>
    <!-- 
    subsystem - Identifies Adtran subsystem to be traced. See Adtran for list of valid subsystems
    scope - Possible values (msg, event, protocol, data, stats,  misc, debug, startup)
    level - Possible values (none, normal, detailed, extensive)
    -->
    <adtn-subs-trace:subsystem>configuration</adtn-subs-trace:subsystem>
    <adtn-subs-trace:subsystem-trace>
        <adtn-subs-trace:scope>msg</adtn-subs-trace:scope>
        <adtn-subs-trace:level>none</adtn-subs-trace:level>
    </adtn-subs-trace:subsystem-trace>
    <adtn-subs-trace:subsystem-trace>
        <adtn-subs-trace:scope>event</adtn-subs-trace:scope>
        <adtn-subs-trace:level>none</adtn-subs-trace:level>
    </adtn-subs-trace:subsystem-trace>
    <adtn-subs-trace:subsystem-trace>
        <adtn-subs-trace:scope>protocol</adtn-subs-trace:scope>
        <adtn-subs-trace:level>none</adtn-subs-trace:level>
    </adtn-subs-trace:subsystem-trace>
    <adtn-subs-trace:subsystem-trace>
        <adtn-subs-trace:scope>data</adtn-subs-trace:scope>
        <adtn-subs-trace:level>none</adtn-subs-trace:level>
    </adtn-subs-trace:subsystem-trace>
    <adtn-subs-trace:subsystem-trace>
        <adtn-subs-trace:scope>stats</adtn-subs-trace:scope>
        <adtn-subs-trace:level>none</adtn-subs-trace:level>
    </adtn-subs-trace:subsystem-trace>
    <adtn-subs-trace:subsystem-trace>
        <adtn-subs-trace:scope>misc</adtn-subs-trace:scope>
        <adtn-subs-trace:level>none</adtn-subs-trace:level>
    </adtn-subs-trace:subsystem-trace>
    <adtn-subs-trace:subsystem-trace>
        <adtn-subs-trace:scope>debug</adtn-subs-trace:scope>
        <adtn-subs-trace:level>none</adtn-subs-trace:level>
    </adtn-subs-trace:subsystem-trace>
    <adtn-subs-trace:subsystem-trace>
        <adtn-subs-trace:scope>startup</adtn-subs-trace:scope>
        <adtn-subs-trace:level>none</adtn-subs-trace:level>
    </adtn-subs-trace:subsystem-trace>
</adtn-subs-trace:trace>
</adtn-subs-trace:traces>

错误如下..

Traceback (most recent call last):
File "C:/Users/uvijayac/Desktop/REST/r3.py", line 149, in <module>
x.main(param_dict)
File "C:/Users/uvijayac/Desktop/REST/r3.py", line 114, in main
root_payload = ET.fromstring(request_dict[file_name]['payload'])
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1300, in XML
parser.feed(text)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1642, in feed
self._raiseerror(v)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror
raise err
xml.etree.ElementTree.ParseError: unbound prefix: line 15, column 0

您的xml没有关闭。在 xml

的末尾添加这一行
</adtn-subs-trace:traces>

将姓名 space 添加到您的 xml。并将 xmlms 更改为 xmlns。以你的名字 space(xmlns = XML 姓名 Space)。

<?xml version="1.0"?>
<adtn-subs-trace:traces xmlns:adtn-subs-trace="http://www.adtran.com/ns/yang/adtran-subsystem-traces">