from xml.dom import minidom contents = "<grammar><ref id='bit'><para>0</para><para>1</para></ref></grammar>" xmldoc = minidom.parseString(contents) print xmldoc.toxml()