Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was looking for exactly the same thing, and I came across this post. (the indenting provided in xml.dom.minidom broke another tool that I was using to manipulate the XML, and I needed it to be indented) I tried the accepted solution with a slightly more complex example and this was the result:</p> <pre><code>In [1]: import pxdom In [2]: xml = '&lt;a&gt;&lt;b&gt;fda&lt;/b&gt;&lt;c&gt;&lt;b&gt;fdsa&lt;/b&gt;&lt;/c&gt;&lt;/a&gt;' In [3]: doc = pxdom.parseString(xml) In [4]: doc.domConfig.setParameter('format-pretty-print', True) In [5]: print doc.pxdomContent &lt;?xml version="1.0" encoding="utf-16"?&gt; &lt;a&gt; &lt;b&gt;fda&lt;/b&gt;&lt;c&gt; &lt;b&gt;fdsa&lt;/b&gt; &lt;/c&gt; &lt;/a&gt; </code></pre> <p>The pretty printed XML isn't formatted correctly, and I'm not too happy about monkey patching (i.e. I barely know what it means, and understand it's bad), so I looked for another solution. </p> <p>I'm writing the output to file, so I can use the xmlindent program for Ubuntu ($sudo aptitude install xmlindent). So I just write the unformatted to the file, then call the xmlindent from within the python program:</p> <pre><code>from subprocess import Popen, PIPE Popen(["xmlindent", "-i", "2", "-w", "-f", "-nbe", file_name], stderr=PIPE, stdout=PIPE).communicate() </code></pre> <p>The -w switch causes the file to be overwritten, but annoyingly leaves a named e.g. "myfile.xml~" which you'll probably want to remove. The other switches are there to get the formatting right (for me). </p> <p>P.S. xmlindent is a stream formatter, i.e. you can use it as follows:</p> <pre><code>cat myfile.xml | xmlindent &gt; myfile_indented.xml </code></pre> <p>So you might be able to use it in a python script without writing to a file if you needed to.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload