0
我想要有它的所有子元素的'post'的五个实例。当我这样做时,它只是覆盖前四个,并留下了第5个帖子。我如何以唯一标识它们的方式添加新元素,以便如果我再次运行该脚本,则会在第1个帖子中添加不同的标题会被覆盖到帖子1?添加一个元素的新实例
#!usr/bin/env python
import xml.etree.ElementTree as xml
#root name and name of the xml file
dasub = 'therootname'
#open the xml file
file = open("/class/myname/"+dasub+".xml", 'w')
valid = 0
#I want 5 instances of 'post' using the number = valid to identify them
while(valid <= 5):
root = xml.Element(dasub)
post = xml.Element('post')
root.append(post)
post.attrib['number'] = str(valid)
title = xml.Element('title')
title.text = "a diffent text for each one here"
post.append(title)
valid = valid + 1
#write it to file
xml.ElementTree(root).write(file)
#close the file
file.close()
非常感谢你! – whuff739 2012-04-24 02:11:00