我試圖從 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>
我正在嘗試使用 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)
for child in root:
print(child.tag, child.attrib)
reply_xml是一個包含上述 xml 的字串,因此它應該可以作業,但是如果我使用除錯器檢查根變數,我會發現它沒有正確填充。似乎第一個 xml 標記 ( <?xml version="1.0" encoding="UTF-8"?>) 產生了一些問題,但即使我手動洗掉它,我也無法正確決議 xml。
有什么線索可以決議那個xml嗎?
uj5u.com熱心網友回復:
您的原始 XML 具有命名空間。您需要在 XPath 查詢中尊重它們。
import xml.etree.ElementTree as ET
reply_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">
<!-- ... the rest of it ... -->
</config>
</data>
</rpc-reply>'''
ns = {
'calix': 'http://www.calix.com/ns/exa/base'
}
root = ET.fromstring(reply_xml)
for eir in root.findall('.//calix:eir', ns):
print(eir.text)
印刷
1000000 10000000
uj5u.com熱心網友回復:
你的代碼作業正常。它顯示了根元素的所有子元素,它只有 <config> .. </config>并且沒有屬性。
要獲得<eir>標簽,您應該使用XPath,或者遞回地遍歷樹。
XPath 的快速解決方案:
root.findall('.//eir')
uj5u.com熱心網友回復:
見下文(1 個帶 xpath 的襯墊)
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>20000000</eir>
</ingress>
</class-map-ethernet>
</policy-map>
</profile>
</config>
</data>
'''
root = ET.fromstring(reply_xml)
eirs = [e.text for e in root.findall('.//eir')]
print(eirs)
輸出
['1000000', '20000000']
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314417.html
下一篇:如何正確評估文字字符向量
