Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Python's ElementTree to pretty print to an XML file?

Background

I am using SQLite to access a database and retrieve the desired information. I'm using ElementTree in Python version 2.6 to create an XML file with that information.

Code

import sqlite3 import xml.etree.ElementTree as ET  # NOTE: Omitted code where I acccess the database, # pull data, and add elements to the tree  tree = ET.ElementTree(root)  # Pretty printing to Python shell for testing purposes from xml.dom import minidom print minidom.parseString(ET.tostring(root)).toprettyxml(indent = "   ")  #######  Here lies my problem  ####### tree.write("New_Database.xml") 

Attempts

I've tried using tree.write("New_Database.xml", "utf-8") in place of the last line of code above, but it did not edit the XML's layout at all - it's still a jumbled mess.

I also decided to fiddle around and tried doing:
tree = minidom.parseString(ET.tostring(root)).toprettyxml(indent = " ")
instead of printing this to the Python shell, which gives the error AttributeError: 'unicode' object has no attribute 'write'.

Questions

When I write my tree to an XML file on the last line, is there a way to pretty print to the XML file as it does to the Python shell?

Can I use toprettyxml() here or is there a different way to do this?

like image 251
Kimbluey Avatar asked Mar 02 '15 15:03

Kimbluey


People also ask

How do I print a pretty XML string in Python?

If your xml exists as a minidom node, you can use the toprettyxml() function. If it really only ever exists as a string, you will have to parse it in before you can pretty print it out.

How do you create an XML file in Python?

Creating XML Document using Python First, we import minidom for using xml. dom . Then we create the root element and append it to the XML. After that creating a child product of parent namely Geeks for Geeks.


2 Answers

Whatever your XML string is, you can write it to the file of your choice by opening a file for writing and writing the string to the file.

from xml.dom import minidom  xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent="   ") with open("New_Database.xml", "w") as f:     f.write(xmlstr) 

There is one possible complication, especially in Python 2, which is both less strict and less sophisticated about Unicode characters in strings. If your toprettyxml method hands back a Unicode string (u"something"), then you may want to cast it to a suitable file encoding, such as UTF-8. E.g. replace the one write line with:

f.write(xmlstr.encode('utf-8')) 
like image 170
Jonathan Eunice Avatar answered Sep 28 '22 07:09

Jonathan Eunice


I simply solved it with the indent() function:

xml.etree.ElementTree.indent(tree, space=" ", level=0) Appends whitespace to the subtree to indent the tree visually. This can be used to generate pretty-printed XML output. tree can be an Element or ElementTree. space is the whitespace string that will be inserted for each indentation level, two space characters by default. For indenting partial subtrees inside of an already indented tree, pass the initial indentation level as level.

tree = ET.ElementTree(root) ET.indent(tree, space="\t", level=0) tree.write(file_name, encoding="utf-8") 

Note, the indent() function was added in Python 3.9.

like image 44
Rafal.Py Avatar answered Sep 28 '22 06:09

Rafal.Py