Cisco

CCNA DevNet Study Guide – Describe parsing of common data format (XML, JSON, YAML) to Python data structures

This is the second post in a series about the new CCNA DevNet certification (Previous Post Here). In this post, we will look at how to manage in Python the three formats that we previously discussed.

Example of XML parsing in Python

from __future__ import print_function
import xml.etree.ElementTree as ET
def main():
    # create element tree object
    with open('xmlfile.xml', 'r') as xmlFile:
        tree = ET.parse(xmlFile)
    # get root element
    root = tree.getroot()
    print("Root Tag: " + root.tag)
    print("Using a for Loop:")
    for child in root:
        print(child.tag)
        for attrib in child:
            print(attrib.tag, end=' ')
            print(attrib.text)

    print("Using Indexes:")
    print(root[0].tag)
    print(root[0][0].tag,end=' ')
    print(root[0][0].text)
    print(root[0][1].tag,end=' ')
    print(root[0][1].text)

    print(root[1].tag)
    print(root[1][0].tag,end=' ')
    print(root[1][0].text)
    print(root[1][1].tag,end=' ')
    print(root[1][1].text)

    print(root[2].tag)
    print(root[2][0].tag,end=' ')
    print(root[2][0].text)
    print(root[2][1].tag,end=' ')
    print(root[2][1].text)

    print("Other:")
    for hostname in root.iter('hostname'):
        print(hostname.tag,end=' ')
        print(hostname.text)

if __name__ == "__main__":
    # calling main function
    main()

Example run of the previous code:

Root Tag: esx
Using a for Loop:
XX1
hostname ESXi01
ipaddress 10.10.10.101
XX2
hostname ESXi02
ipaddress 10.10.10.102
XX3
hostname ESXi03
ipaddress 10.10.10.103
Using Indexes:
XX1
hostname ESXi01
ipaddress 10.10.10.101
XX2
hostname ESXi02
ipaddress 10.10.10.102
XX3
hostname ESXi03
ipaddress 10.10.10.103
Other:
hostname ESXi01
hostname ESXi02
hostname ESXi03

This is the XML file we used:

<?xml version="1.0" encoding="UTF-8"?>
<esx>
<XX1>
<hostname>ESXi01</hostname>
<ipaddress>10.10.10.101</ipaddress>
</XX1>
<XX2>
<hostname>ESXi02</hostname>
<ipaddress>10.10.10.102</ipaddress>
</XX2>
<XX3>
<hostname>ESXi03</hostname>
<ipaddress>10.10.10.103</ipaddress>
</XX3>
</esx>

To achieve something similar with the .json file we would use “import json”

import json

def main():
    with open('jason.json', 'r') as jsonFile:
        #load jason file
        myJasonFile = json.load(jsonFile)
    print(myJasonFile)

if __name__ == "__main__":
    # calling main function
    main()

And to parse YAML

import yaml

with open("yamlfile.yaml", 'r') as yamlFile:
    try:
        print(yaml.safe_load(yamlFile))
    except yaml.YAMLError as exc:
        print(exc)

Let us read the RSS feed (XML) directly from a Website (https://vwannabe.com/feed/)*

from urllib.request import urlopen
from xml.etree.ElementTree import parse

myURL = urlopen("https://vwannabe.com/feed/")
myXML=parse(myURL)

for item in myXML.iterfind('channel/item'):
title = item.findtext('title')
date = item.findtext('pubDate')
link = item.findtext('link')

print(title)
print(date)
print(link)
print()

*adapted from “Python – How to Read XML from URL?” by Vinish Kapoor

This is the result of the previous code:

CCNA DevNet Study Guide – Part 1
Sun, 19 Jan 2020 17:17:45 +0000
https://vwannabe.com/2020/01/19/ccna-devnet-study-guide-part-1/
vSphere Upgrade 6.0 to 6.5 Fails with Replace Process Level Token error.
Wed, 19 Jun 2019 15:32:13 +0000
https://vwannabe.com/2019/06/19/vsphere-upgrade-6-0-to-6-5-fails-with-replace-process-level-token-error/
Vembu now supports Hyper-V Cluster
Thu, 01 Nov 2018 12:25:42 +0000
https://vwannabe.com/2018/11/01/vembu-now-supports-hyper-v-cluster/
vCenter 6.7 upgrade walkthrough
Fri, 20 Apr 2018 19:26:54 +0000
https://vwannabe.com/2018/04/20/vcenter-6-7-upgrade-walkthrough/
Vembu
Wed, 28 Mar 2018 19:11:09 +0000
https://vwannabe.com/2018/03/28/vembu-bdr-suite/
How to re-register the embedded VMware Update Manager (VUM) to its vCenter (VCSA) 6.5
Wed, 21 Feb 2018 23:06:36 +0000
https://vwannabe.com/2018/02/21/how-to-re-register-the-embedded-vmware-update-manager-vum-to-its-vcenter-vcsa-6-5/
How to spin up a Linux instance in AWS
Thu, 08 Feb 2018 20:57:06 +0000
https://vwannabe.com/2018/02/08/how-to-spin-up-a-linux-instance-in-aws/
CCNA Cyber Ops – SECOPS 1.0
Tue, 02 Jan 2018 20:23:21 +0000
https://vwannabe.com/2018/01/02/ccna-cyber-ops-secops-1-0/
Hacking Public Speaking
Wed, 30 Aug 2017 16:50:28 +0000
https://vwannabe.com/2017/08/30/hacking-public-speaking/
VMworld 2017 General Session Day Two
Tue, 29 Aug 2017 17:44:05 +0000
https://vwannabe.com/2017/08/29/vmworld-2017-general-session-day-two/

The CISCO Blueprint uses REST calls to a site and parses the JSON. You can find the example here. In the next port for this series, I will talk about “Describe the concepts of test-driven development”.

One thought on “CCNA DevNet Study Guide – Describe parsing of common data format (XML, JSON, YAML) to Python data structures

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.