07-22-2024 04:10 AM
Hi,
I wrote a short article on why we should use native XML modules to parse XML data in Python. You can find the article on GitHub: https://github.com/maercu/ciscosupportexamples/blob/main/xml_processing/README.md.
It might be interesting for those of you dealing with NETCONF/XML parsing.
07-22-2024 05:53 AM
Amazing mate!
07-25-2024 09:10 AM
If you already know the structure of XML document then you can access children of XML element like elements of list. Maybe it's easier to make it like this:
In [1]: import xml.etree.ElementTree as ET
In [2]: text_string = """\
...: <data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
...: <interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper">
...: <interface>
...: <name>GigabitEthernet1</name>
...: <oper-status>if-oper-state-ready</oper-status>
...: </interface>
...: <interface>
...: <name>GigabitEthernet2</name>
...: <oper-status>if-oper-state-no-pass</oper-status>
...: </interface>
...: <interface>
...: <name>GigabitEthernet3</name>
...: <oper-status>if-oper-state-no-pass</oper-status>
...: </interface>
...: </interfaces>
...: </data>
...: """
In [3]: data = ET.fromstring(text_string)
In [4]: for element in data.iter("{http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper}interface"):
...: print(f"Interface: {element[0].text:<20} oper-status: {element[1].text:<20}")
...:
Interface: GigabitEthernet1 oper-status: if-oper-state-ready
Interface: GigabitEthernet2 oper-status: if-oper-state-no-pass
Interface: GigabitEthernet3 oper-status: if-oper-state-no-pass
07-25-2024 11:36 PM - edited 07-25-2024 11:41 PM
Very good article. Some time ago I had similar issue, where xmltodict parser parsed the data to the json and there was a case where specific structure (like interfaces in your case) contained only one element.
The result in json was similar to yours - specific element, instead of the list that contained that element was returned. This is annoying when you can't get consistent data. I don't like approach with working on pure XML using ET as it's not as convenient as working with JSON (just my opinion), but your article makes me think that in the next project i should consider it. Awesome write up!
This also makes me think about selecting proper approach. It really depends what is the overhead in the whole project on dealing with things like this and how many "if" statements you need to handle in the codebase because sometimes parsed json structure doesn't provide the elements in consistent way.
On the other hand - we would need to deal with learning curve of XML and how to parse the data using XML ET - it requires understanding the basics of XML (JSON team here!), the team needs to understand the ET framework and so on. This approach also pose a risk of huge codebase because of the XML ET nature and how it works.
As always - dealing with tradeoffs, no ideal solution in this case. I always love this kind of considerations, because i see other possible options that solves the issue in a different way rather that being sticked to the one approach, that I really understand.
Thanks!
07-29-2024 03:34 AM - edited 07-29-2024 03:40 AM
Hi @mfr-6 thanks four your nice feedback!
I think we're on the same page - I'm also team JSON and use xmltodict often - It's just recently I switched to ET, and still need to force myself everytime using it. That's why I like to use RESTCONF, but compared to NETCONF it has its drawbacks - specially in large scale automation projects.
It's the never ending IT story: Learn something new every day.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide