Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

sys.exit() fileToParse = sys.argv[ 1 ] print ( \'File to Parse:{}\' .format(file

ID: 3796654 • Letter: S

Question


    sys.exit()

fileToParse = sys.argv[1]
print('File to Parse:{}'.format(fileToParse))
tree = ET.ElementTree(
file=fileToParse)
print("*****")
print("Tree Root:")
root = tree.getroot()

print("root tag:{}, root attribute:{}".format(root.tag, root.attrib))

print("*****")
print("Children of root")
for element in root:
   
print (element.tag, element.attrib, element.keys(), element.text)

print("*****")
itemsInTree =
len(root)
print("Print Indexed Items")
print("First Item tag: {}, First Item attribute: {}".format(root[0].tag, root[0].attrib))
print("Second Item tag: {}, Second Item attribute: {}".format(root[1].tag, root[1].attrib))
print("Last Item tag: {}, Last Item attribute: {}".format(root[itemsInTree-1].tag, root[itemsInTree-1].attrib) )

print("*****")
print("Iterate through entire tree, providing a list of branch/sub-branch tags")
for element in tree.findall('branch/sub-branch'):
   
print (element.tag, element.attrib)

print("*****")
print("Deleting second item")
root.remove(root[
1])

print("*****")
print("Insert an item at the end")
print ("Last Item:")

newElement = ET.Element(
'branch')
newElement.set(
'name', "Newly Added Name")
newElement.text=
"This is a test"
root.append(newElement)

outfile = open('testxmlout.xml', 'w')
print("*****")
print("Print a formatted tree (reflecting changes)")
xmlstrPretty = parseString(ET.tostring(root)).toprettyxml(
indent="   ", newl=' ')
print (xmlstrPretty)
outfile.write(xmlstrPretty)

print("*****")
print("Convert to Dict")
xmlFile =
open('doc1.xml', "rb") # notice the "rb" mode
dictObject = xmltodict.parse(xmlFile, xml_attribs=True)
print(dictObject)
print (dictObject['doc']['branch'][0]['#text'])

for branch in dictObject['doc']['branch']:
   
print (branch['@name'], " ", branch['#text'])

print("*****")
print("Convert to json and back to a dictionary")
jsonObject = json.dumps(dictObject,
indent=4)
dictJsonObject = json.loads(jsonObject)

print(dictJsonObject)
print(dictJsonObject['doc']['branch'][0]['#text'])

Explanation / Answer

#I want only comments in this python code to understand the codes.


import sys

import xml.etree.ElementTree as ET

from xml.dom.minidom import parseString

import json

import xmltodict

''' Reading sys arg and checking if it is -h or --help'''

if (sys.argv[1]=="-h" or sys.argv[1]=="--help"):

print("USAGE: python3 {} inputfile.xml ".format(sys.argv[0]))

sys.exit()

''' reading the file name that need to be parsed '''
fileToParse = sys.argv[1]
print('File to Parse:{}'.format(fileToParse))

''' The element tree is used to wrap an element structure, and convert it from and to XML.'''
''' creating a tree with all the elements of the xml '''
tree = ET.ElementTree(file=fileToParse)
print("*****")
print("Tree Root:")

''' Getting the root tag from the given xml '''
root = tree.getroot()
print("root tag:{}, root attribute:{}".format(root.tag, root.attrib))


print("*****")
print("Children of root")

''' Printing all the values of a particular element from the xml '''
for element in root:
print (element.tag, element.attrib, element.keys(), element.text)

print("*****")
''' Finding the number of items in the xml '''
itemsInTree = len(root)
print("Print Indexed Items")
print("First Item tag: {}, First Item attribute: {}".format(root[0].tag, root[0].attrib))
print("Second Item tag: {}, Second Item attribute: {}".format(root[1].tag, root[1].attrib))
print("Last Item tag: {}, Last Item attribute: {}".format(root[itemsInTree-1].tag, root[itemsInTree-1].attrib) )

print("*****")
print("Iterate through entire tree, providing a list of branch/sub-branch tags")

''' Finding all the branch and the sub branches of a given element from the excel '''
for element in tree.findall('branch/sub-branch'):
print (element.tag, element.attrib)

print("*****")
print("Deleting second item")
root.remove(root[1])

print("*****")
print("Insert an item at the end")
print ("Last Item:")

newElement = ET.Element('branch')
newElement.set('name', "Newly Added Name")
newElement.text="This is a test"
root.append(newElement)

''' Opening a file (if it exists already if not it ll create one) '''
outfile = open('testxmlout.xml', 'w')
print("*****")
print("Print a formatted tree (reflecting changes)")
xmlstrPretty = parseString(ET.tostring(root)).toprettyxml(indent=" ", newl=' ')
print (xmlstrPretty)
''' Writing the output to the file'''
outfile.write(xmlstrPretty)

print("*****")
print("Convert to Dict")
xmlFile = open('doc1.xml', "rb") # notice the "rb" mode
dictObject = xmltodict.parse(xmlFile, xml_attribs=True)
print(dictObject)
print (dictObject['doc']['branch'][0]['#text'])

for branch in dictObject['doc']['branch']:
print (branch['@name'], " ", branch['#text'])

print("*****")
print("Convert to json and back to a dictionary")
''' Converting the jsoj objects to a dictionary '''
jsonObject = json.dumps(dictObject, indent=4)
dictJsonObject = json.loads(jsonObject)
print(dictJsonObject)
print(dictJsonObject['doc']['branch'][0]['#text'])