python XML解析错误:' ParseError:未绑定的前缀:' (无法找到xml文件的任何问题)

时间:2017-03-22 10:10:33

标签: xml python-2.7 elementtree

您好我在解析以下xml文件时遇到以下错误,(无法找到xml文件的任何问题)。我希望这个xml在没有失败的情况下传递解析。 我使用xml.etree进行解析,我在etree中使用formtring方法。 我正在使用:

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

1 个答案:

答案 0 :(得分:0)

您的xml未关闭。添加xml的这一行

</adtn-subs-trace:traces>

为xml添加名称空间。并将xmlms更改为xmlns。在您的名称空间中(xmlns = XML名称空间)。

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