DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1

that a tag acts as a key with a value. You can also assign
attributes to a tag by using the following syntax:


attribute name="some value"

This works the same as an element in that it can provide
a way to represent data. Example 5-4 shows an example
of an IETF interface YANG model in XML.


Example 5-4 YANG Model Represented in XML

Click here to view code image


<?xml version="1.0" encoding="UTF-8" ?>
<interface xmlns="ietf-interfaces">
<name>GigabitEthernet2</name>
<description>Wide Area Network</description>
<enabled>true</enabled>
<ipv4>
<address>
<ip>192.168.1.5</ip>
<netmask>255.255.255.0</netmask>
</address>
</ipv4>
</interface>

To work with this, you can use the native XML library,
but it has a bit of a learning curve and can be a little hard
to use if you just want to convert XML into something
you can work with in Python. To make it easier, you can
use a module called xmltodict to convert XML into an
ordered dictionary in Python. This is a special class of
dictionary that does not allow elements to change order.
Since dictionaries use key/value pairs, where the
key/value pairs are stored is normally not a problem, but
in the case of XML, order matters. Example 5-5 reads in
the XML from Example 5-4 and converts it to an ordered
dictionary.


Example 5-5 Reading in XML and Printing the
Imported Dictionary to the Command Line

Free download pdf