0

I've looked at: Pretty printing XML in Python This doesn't suit my needs, because for various reasons - I've got a base install of Python 3.2 on Windows, but no ability to add modules. (user privs).

The suggested solution is:

import xml.dom.minidom

xml = xml.dom.minidom.parse(xml_fname) 
pretty_xml_as_string = xml.toprettyxml()

This turns:

<root>
    <node>content</node>
    <node attrib="value">more content</node>
</root>

Into:

<?xml version="1.0" ?>
<root>


        <node>content</node>


        <node attrib="value">more content</node>


</root>

It does work if you've got:

<root><node>content</node><node attrib="value">more content</node></root>

So I would assume it's preserving linefeeds, but adding extra indent/whitespace. I appreciate the answer is probably 'use a module' - but what I'm looking for is something that can turn arbitrary formatted xml into consistently pretty-printed.

(And using a core install module if at all possible).

Community
  • 1
  • 1
Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • Was your input generated under Windows? If so then it is possible that `toprettyxml` expects UNIX-style line endings (LF). Try replacing in the input all CRLF with LF. – avnr Oct 21 '15 at 15:56
  • It was, yes. I shall try that. – Sobrique Oct 21 '15 at 16:00

0 Answers0