擅长:python、mysql、java
<p>如果希望XML文本文件的缩进直观地表示XML文档的层次结构,则需要将其打印出来。一种方法是使用<code>xmllint --format</code>:</p>
<pre><code>$ xmllint --format test.xml
<?xml version="1.0"?>
<testcases>
<mode>PRESSURE_CONTROL</mode>
<category>ADULT</category>
<testcase id="1">
<parameter id="PEEP" value="1.000000">false</parameter>
<parameter id="CMV_FREQ" value="4.0">false</parameter>
<parameter id="PRESS_ABOVE_PEEP" value="0.0">true</parameter>
<parameter id="I_E_RATIO" value="0.100000">false</parameter>
</testcase>
<invalid>
<testcase id="5" parameter="I_E_RATIO">100.0</testcase>
</invalid>
</testcases>
</code></pre>
<p>如果要生成已经打印好的文本文件,请尝试使用不同的XML库(例如minidom:</p>
<pre><code>>>> print minidom.parseString(
ET.tostring(
tree.getroot(),
'utf-8')).toprettyxml(indent=" ")
</code></pre>
<p>但是请注意,每个解决方案都会更改XML文档。严格地说
生成的文本文件不等同于原始文本文件——文本元素添加了额外的空格和换行符。</p>