Skip to content

Instantly share code, notes, and snippets.

@narate
Last active January 22, 2016 15:20
Show Gist options
  • Save narate/5e818b844f9a4758b1b1 to your computer and use it in GitHub Desktop.
Save narate/5e818b844f9a4758b1b1 to your computer and use it in GitHub Desktop.
Pretty XML string
#!/usr/bin/env python
import sys
import xml.dom.minidom
data = sys.stdin.read()
xml = xml.dom.minidom.parseString(data)
print xml.toprettyxml(indent=" ", newl="")
@narate
Copy link
Author

narate commented Jan 22, 2016

Example.

$ ls
a.xml pretty_xml.py
$ cat a.xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetCervotSSByPID xmlns="http://tempuri.org/">
<PID>long</PID>
</GetCervotSSByPID>
</soap:Body>
</soap:Envelope>
$ cat a.xml | python pretty_xml.py
<?xml version="1.0" ?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  <soap:Body>    
    <GetCervotSSByPID xmlns="http://tempuri.org/">      
      <PID>long</PID>      
    </GetCervotSSByPID>    
  </soap:Body>  
</soap:Envelope>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment