Python – ElementTree- cannot use absolute path on element

Turns out I needed to say target.findall(“.//StepText”). I guess anything without the ‘.’ is considered an absolute path? Updated working code: def search(): root = ET.parse(INPUT_FILE_PATH) for target in root.findall(“//Script”): stepTexts = target.findall(“.//StepText”) for stepText in stepTexts: if FIND.lower() in stepText.text.lower(): print target.attrib[‘name’],’ — ‘,stepText.text

How to get all sub-elements of an element tree with Python ElementTree?

All sub-elements (descendants) of elem: all_descendants = list(elem.iter()) A more complete example: >>> import xml.etree.ElementTree as ET >>> a = ET.Element(‘a’) >>> b = ET.SubElement(a, ‘b’) >>> c = ET.SubElement(a, ‘c’) >>> d = ET.SubElement(a, ‘d’) >>> e = ET.SubElement(b, ‘e’) >>> f = ET.SubElement(d, ‘f’) >>> g = ET.SubElement(d, ‘g’) >>> [elem.tag for elem …

Read more

Converting xml to dictionary using ElementTree

The following XML-to-Python-dict snippet parses entities as well as attributes following this XML-to-JSON “specification”: from collections import defaultdict def etree_to_dict(t): d = {t.tag: {} if t.attrib else None} children = list(t) if children: dd = defaultdict(list) for dc in map(etree_to_dict, children): for k, v in dc.items(): dd[k].append(v) d = {t.tag: {k: v[0] if len(v) == …

Read more

Using Python Iterparse For Large XML Files

Try Liza Daly’s fast_iter. After processing an element, elem, it calls elem.clear() to remove descendants and also removes preceding siblings. def fast_iter(context, func, *args, **kwargs): “”” http://lxml.de/parsing.html#modifying-the-tree Based on Liza Daly’s fast_iter http://www.ibm.com/developerworks/xml/library/x-hiperfparse/ See also http://effbot.org/zone/element-iterparse.htm “”” for event, elem in context: func(elem, *args, **kwargs) # It’s safe to call clear() here because no descendants …

Read more

How can I check the existence of attributes and tags in XML before parsing?

If a tag doesn’t exist, .find() indeed returns None. Simply test for that value: for event in root.findall(‘event’): party = event.find(‘party’) if party is None: continue parties = party.text children = event.get(‘value’) You already use .get() on event to test for the value the attribute; it returns None as well if the attribute does not …

Read more

Using XPath in ElementTree

There are 2 problems that you have. 1) element contains only the root element, not recursively the whole document. It is of type Element not ElementTree. 2) Your search string needs to use namespaces if you keep the namespace in the XML. To fix problem #1: You need to change: element = ET.parse(fp).getroot() to: element …

Read more