Use xml.etree.ElementTree to print nicely formatted xml files [duplicate]

You can use the function toprettyxml() from xml.dom.minidom in order to do that: def prettify(elem): “””Return a pretty-printed XML string for the Element. “”” rough_string = ElementTree.tostring(elem, ‘utf-8’) reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent=”\t”) The idea is to print your Element in a string, parse it using minidom and convert it again in XML using the … Read more

How do I parse and write XML using Python’s ElementTree without moving namespaces around?

As far as I know the solution that better suits your needs is to write a pure Python custom rendering using the features exposed by xml.etree.ElementTree. Here is one possible solution: from xml.etree import ElementTree as ET from re import findall, sub def render(root, buffer=””, namespaces=None, level=0, indent_size=2, encoding=’utf-8′): buffer += f'<?xml version=”1.0″ encoding=”{encoding}” ?>\n’ … Read more

get the namespaces from xml with python ElementTree

The code for creating a dictionary with all the declared namespaces can be made quite simple. This is all that is needed: import xml.etree.ElementTree as ET my_namespaces = dict([node for _, node in ET.iterparse(‘file.xml’, events=[‘start-ns’])]) You don’t need to use StringIO or open(). Just provide the XML filename as an argument to iterparse(). Each item … Read more

Using SimpleXMLTreeBuilder in elementtree

If you have third party module that wants to use ElementTree (and XMLTreeBuilder by dependency) you can change ElementTree’s XMLTreeBuilder definition to the one provided by SimpleXMLTreeBuilder like so: from xml.etree import ElementTree # part of python distribution from elementtree import SimpleXMLTreeBuilder # part of your codebase ElementTree.XMLTreeBuilder = SimpleXMLTreeBuilder.TreeBuilder Now ElementTree will always use … Read more

Parsing compressed xml feed into ElementTree

You can pass the value returned by urlopen() directly to GzipFile() and in turn you can pass it to ElementTree methods such as iterparse(): #!/usr/bin/env python3 import xml.etree.ElementTree as etree from gzip import GzipFile from urllib.request import urlopen, Request with urlopen(Request(“http://smarkets.s3.amazonaws.com/oddsfeed.xml”, headers={“Accept-Encoding”: “gzip”})) as response, \ GzipFile(fileobj=response) as xml_file: for elem in getelements(xml_file, ‘interesting_tag’): process(elem) … Read more