Python Elementtree SubElement不正确

时间:2013-05-22 22:11:47

标签: python elementtree

我正在尝试将SubElement添加到单独的SubElement中,我无法弄清楚为什么输出xml输出不正确。

import xml.etree.cElementTree as ET

def CreateXml(list):
    Channel = ET.Element("Channel")
    cell    = ET.SubElement(Channel,"cell")   
    pngIcc0 = ET.SubElement(cell,"InputIcc0")
    pngIcc0.set("StructName","pngImage")
    pngIcc0.text = "DataBuffer_t"
    type0   = ET.SubElement(pngIcc0,"DataType")
    type0.set("Type","dataBuffer_t")
    pngOut0 = ET.SubElement(cell,"OutputIcc0")
    pngOut0.set("StructName","rawImage")
    pngOut0.text = "DataBuffer_t"
    tree = ET.ElementTree(Channel)
    tree.write("E:\Programming/ChannelCreation.xml")

结果xml看起来像

<InputIcc0 StructName="pngImage">
     DataBuffer_t
 <DataType Type="dataBuffer_t"/>
</InputIcc0>

如果我想让Type0成为pngIcc0的孩子,那么这是一个正确的方法吗?或者我是否完全错了。我对xml的python没有多少经验。

0 个答案:

没有答案