Blog Logo
  • Home
  • About

Pretty print xml (and json) in Python

by Drew Engelson — 20 Oct 2010

So that I don't forget... again.

from xml.dom import minidom

# from a string
minidom.parseString(xml_string).toprettyxml(indent=' '*4)

# or from a file
minidom.parse(open(xml_file)).toprettyxml(indent=' '*4)

And for the record...

import simplejson as json

json.dumps(json_obj, indent=4)
Drew Engelson
Author

Drew Engelson

https://tomatohater.com

I currently lead the Cloud Foundation Services team at Starbucks. We are building the Starbucks PaaS to allow digital platforms to be deployed globally with a focus on automation, scalability, performance, security, and developer experience.

Prior to Starbucks, I led Celerity's Technology Innovation and Architecture practice and founded Zoombit and served in technology leadership at Huge and PBS.

Yes. I seriously hate tomatoes.

All content copyright Tomatohater © 2006-2021 • All rights reserved.
No ResultWebsite Carbon