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
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
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
Vembu now supports Hyper-V Cluster
vCenter 6.7 upgrade walkthrough Fri, 20 Apr 2018 19:26:54 +0000
vCenter 6.7 upgrade walkthrough
Vembu Wed, 28 Mar 2018 19:11:09 +0000
Vembu
How to re-register the embedded VMware Update Manager (VUM) to its vCenter (VCSA) 6.5 Wed, 21 Feb 2018 23:06:36 +0000
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
How to spin up a Linux instance in AWS
CCNA Cyber Ops – SECOPS 1.0 Tue, 02 Jan 2018 20:23:21 +0000
CCNA Cyber Ops – SECOPS 1.0
Hacking Public Speaking Wed, 30 Aug 2017 16:50:28 +0000
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 response to “CCNA DevNet Study Guide – Describe parsing of common data format (XML, JSON, YAML) to Python data structures”

  1. […] Describe parsing of common data format (XML, JSON, YAML) to Python data structures […]

Leave a comment

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

Trending