用python Elemen格式化xml

2024-04-30 04:05:44 发布

您现在位置:Python中文网/ 问答频道 /正文

XML文件:

<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>
</testcases>

Python代码:

import xml.etree.ElementTree as ET

tree = ET.parse('Results.xml')    
root = tree.getroot()

mode = root.find('Mode').text
category = root.find('Category').text

        self.tag_invalid = ET.SubElement(root, 'invalid')    # For adding new  tag with attributes and values      
        for v in self.final_result:
            self.tag_testcase = ET.SubElement(self.tag_invalid, 'testcase')
            self.tag_testcase.attrib['id'] = 5
            self.tag_testcase.attrib['parameter'] = 'IE'
            self.tag_testcase.text = 100
            tree.write('/home/AlAhAb65/Desktop/test.xml')

输出:

<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>  # Extra line after python code running

额外的一行被添加到XML文件中。但问题是我无法格式化它。这意味着我不能添加“\n”、“\t”来维护层次结构和格式。有什么规定吗?我试过tree.write(),ET.Element()函数。但这些并不能提供理想的结果。


Tags: selfidfalsetreeparametervaluemodetag
3条回答

根据ET manual

Writes an element tree or element structure to sys.stdout. This function should be used for debugging only.

The exact output format is implementation dependent. In this version, it’s written as an ordinary XML file.

但谷歌上有are some fixes for that

可以使用属性tailtext控制ElementTree元素的文本内容。E、 g.尝试添加:

self.tag_invalid.text = "\n    "
self.tag_invalid.tail = "\n      "

以此为起点,尝试将文本/尾部添加到您创建的其他各种元素中,打印结果,并对其进行处理,直到它满足您的需要。

下面是一个示例,显示文本和尾部的含义:

<A>TEXT_OF_A<B>TEXT_OF_B</B>TAIL_OF_B<C>TEXT_OF_C</C>TAIL_OF_C<D/>TAIL_OF_D</A>TAIL_OF_A

或者,可以编写遍历xml树的递归函数,将文本和尾部属性设置为适当缩进(相对于深度)。

有关texttail属性的更多文档,请参见:http://docs.python.org/2/library/xml.etree.elementtree.html

编辑:查看http://effbot.org/zone/element-lib.htm#prettyprint以查看如何递归遍历xml树的示例,设置text&tail以使所有元素都缩进到其嵌套深度。

如果希望XML文本文件的缩进直观地表示XML文档的层次结构,则需要将其打印出来。一种方法是使用xmllint --format

$ 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>

如果要生成已经打印好的文本文件,请尝试使用不同的XML库(例如minidom:

>>> print minidom.parseString(
            ET.tostring(
              tree.getroot(),
              'utf-8')).toprettyxml(indent=" ")

但是请注意,每个解决方案都会更改XML文档。严格地说 生成的文本文件不等同于原始文本文件——文本元素添加了额外的空格和换行符。

相关问题 更多 >