import os
import sys
# using minidom
try:
from xml.dom import minidom
except Exception as e:
print(str(e))
else:
print("Xml dom loaded")
file_in = sys.argv[1]
# parse xml file by name
try:
mydoc = minidom.parse(file_in)
except Exception as e:
print(str(e))
else:
print("Xml file parsed")
finally:
print(str(mydoc))
# Get all elements by specific tag into a list
try:
items = mydoc.getElementsByTagName('item')
except Exception as e:
print(str(e))
else:
print("Parsed specific tag name")
finally:
print(str(items))
#print(dir(items.attributes))
# Access values of the elements in the previous list
try:
print(items[0].attributes['name'].value)
print(items[1].attributes['name'].value)
print(items[2].attributes['name'].value)
except Exception as e:
print(str(e))
# Alternatively, iterate to print all values
try:
for item in items:
print(item.attributes['name'].value)
except Exception as e:
print(str(e))
# Access data of child elements from parent element
try:
print(items[0].firstChild.data)
print(items[0].childNodes[0].data)
print(items[1].firstChild.data)
print(items[1].childNodes[0].data)
print(items[2].firstChild.data)
print(items[2].childNodes[0].data)
except Exception as e:
print(str(e))
# Various ways of accessing data using minidom interface
try:
print(items[0])
print(items[0].firstChild)
print(items[0].firstChild.data)
print(items[0].childNodes)
print(items[0].childNodes[0])
print(items[0].childNodes[0].data)
except Exception as e:
print(str(e))
# Using firstchild and iteration to print all children data.
try:
for elem in items:
print(elem.firstChild.data)
except Exception as e:
print(str(e))
try:
for elem in items:
print(elem.firstChild)
except Exception as e:
print(str(e))
# different way to open the file
try:
datasource = open(file_in)
mydoc2 = minidom.parse(datasource)
except Exception as e:
print(str(e))
import os
import sys
# Using this functions we can count the amount of items of a specific tag
def countxmldomsim(file_in, tag_name):
'''
Name : countxmldomsim
Function : countxmldomsim(file_in, tag_name)
Description : Counts number of tags in an XML document.
Returns the integer count, or false.
Input : <file_in> -> Xml File (.xml)
<tag_name> -> String: ie 'item'
Output : <integer> -> count | boolean False
Usage : countxmldomsim(file_in, 'item')
Notes : In case the parser fails to load the
xml file this function will return false.
'''
import sys
try:
from xml.dom import minidom
except Exception as e:
print("Failed to load necessary modules")
return False
else:
mydoc = minidom.parse(file_in)
try:
items = mydoc.getElementsByTagName(str(tag_name))
except Exception as e:
print("Failed to parse items")
return False
else:
return len(items)
from xml.dom import minidom
file_in = sys.argv[1]
mydoc = minidom.parse(file_in)
items = mydoc.getElementsByTagName('item')
print(len(items))
print(str(countxmldomsim(file_in, 'item')))
import os
import sys
file_in = sys.argv[1]
from xml.dom import minidom
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
from xml.dom import minidom
import xml.etree.ElementTree as ET
rough_string = ET.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")