# # program to prettyprint an XML tree # import xml.etree.ElementTree as ET import sys # # by how many spaces should a child be indented from its parent? # factor = 4 # # put the attributes into a string # make sure there is no trailing space # def print_node_attributes(node): strout = '' # now add on the attributes for key in node.attrib: strout += '%s="%s" ' % (key, node.attrib[key]) # return this, less any trailing spaces strout2 = " " + strout.strip() return strout2 # # go through the XML tree; we do this recursively # def find_kids(node, depth): global factor # build the indent; factor spaces for each level indent = '' for i in range(factor * depth): indent += ' ' # print the opening tag + attributes print(indent + "<" + node.tag, end='') if node.attrib: attrstr = print_node_attributes(node) else: attrstr = '' if node.text: nodetxt = node.text else: nodetxt = '' # see if there are any kids # assume no children initially anykids = False for child in node: # at least one -- drop out anykids = True; break; if not anykids: # no -- close the tag and element if node.text: print(attrstr + ">" + nodetxt + "") else: print(attrstr + "/>") else: # yes -- close tag, leave element open # check that nodetext is not a newline if nodetxt == '\n': print(attrstr + ">") else: print(attrstr + ">" + nodetxt) # now print the kids for child in node: find_kids(child, depth + 1) # and close the element print(indent + "") # load the XML into memory # fptr = open("books.xml", "r") tree = fptr.read() xmltreeroot = ET.fromstring(tree) # # print the tree # find_kids(xmltreeroot, 0)