For some reason I always get stuck with finding children in an XML file especially if there are multiple matches with different attributes. Hence this script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# -*- coding: utf-8 -*- ## Copyright 2019 Trevor van Hoof and Jan Pijpers. ## Licensed under the Apache License, Version 2.0 ## Downloaded from https://janpijpers.com or https://gumroad.com/janpijpers ## See the license file attached or on https://www.janpijpers.com/script-licenses/ ''' Name: xmlPrettyAndUtils. Description: just some example xml utilities ''' import xml.dom.minidom from xml.etree import cElementTree def toPrettyXml(root): if not type(root).__module__.startswith('lxml'): text = cElementTree.tostring(root) text = xml.dom.minidom.parseString(text).toprettyxml() text = text.replace('\r', '\n') else: text = cElementTree.tostring(root, pretty_print=True) return '\n'.join(line for line in text.split('\n') if line and line.strip()) def xmlFindChildren(xParent, tagName=None, **attributesToMatch): iterator = xParent if tagName is not None: iterator = xParent.findall(tagName) children = [] for xChild in iterator: for key, val in attributesToMatch.iteritems(): if xChild.attrib.get(key, None) != val: break else: # all attributes matched children.append(xChild) return children ## Example load from file #filePath = r"C:\xmlfile.xml" #with open(filePath, "r") as xFile: # root = cElementTree.parse(xFile) ## Example load from string xmlData = '''<?xml version="1.0" encoding="UTF-8"?><note><entry attribute="test">This</entry> <entry attribute2="test">is</entry> <body>Hoofd schouders knieen teen...</body></note>''' root = cElementTree.fromstring(xmlData) print "Find child", xmlFindChildren(root,"entry", attribute="test") print print "Pretty XML from flat XML" print "#"*20 print print toPrettyXml(root) |