我正在尝试从 Python 中的字符串解析 XML,但没有成功。我试图解析的字符串是:
<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:573a453c-72c0-4185-8c54-9010593dd102">
<data>
<config xmlns="http://www.calix.com/ns/exa/base">
<profile>
<policy-map>
<name>ELINE_PM_1</name>
<class-map-ethernet>
<name>Eth-match-any-1</name>
<ingress>
<meter-type>meter-mef</meter-type>
<eir>1000000</eir>
</ingress>
</class-map-ethernet>
</policy-map>
<policy-map>
<name>ELINE_PM_2</name>
<class-map-ethernet>
<name>Eth-match-any-2</name>
<ingress>
<meter-type>meter-mef</meter-type>
<eir>10000000</eir>
</ingress>
</class-map-ethernet>
</policy-map>
</profile>
</config>
</data>
</rpc-reply>
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 xml.etree.ElementTree 库来解析 xml,并且我还尝试删除与 xml 版本和编码相关的第一行,但没有结果。重现我面临的问题的代码片段是:
import xml.etree.ElementTree as ET
reply_xml='''
<data>
<config>
<profile>
<policy-map>
<name>ELINE_PM_1</name>
<class-map-ethernet>
<name>Eth-match-any-1</name>
<ingress>
<meter-type>meter-mef</meter-type>
<eir>1000000</eir>
</ingress>
</class-map-ethernet>
</policy-map>
<policy-map>
<name>ELINE_PM_2</name>
<class-map-ethernet>
<name>Eth-match-any-2</name>
<ingress>
<meter-type>meter-mef</meter-type>
<eir>10000000</eir>
</ingress>
</class-map-ethernet>
</policy-map>
</profile>
</config>
</data>
'''
root = ET.fromstring(reply_xml) …Run Code Online (Sandbox Code Playgroud)