I'm trying to make an application to create a xml file and I want to assign a text to certain elements. This text consists on image files on a folder. The code is as follows:
import glob
import os
import os.path
from xml.etree import ElementTree
from xml.dom import minidom
import xml.etree.ElementTree as ET
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
path = "/home/unkuiri/Ubuntu One/Wallpapers/*"
background = ET.Element('background')
starttime = ET.SubElement(background, 'starttime')
year = ET.SubElement(starttime, 'year')
month = ET.SubElement(starttime, 'month')
day = ET.SubElement(starttime, 'day')
hour = ET.SubElement(starttime, 'hour')
minute = ET.SubElement(starttime, 'minute')
second = ET.SubElement(starttime, 'second')
static = ET.SubElement(background, 'static')
duration_stat = ET.SubElement(static, 'duration')
files = ET.SubElement(static, 'file')
transition = ET.SubElement(background, 'transition')
duration_trans = ET.SubElement(transition, 'duration')
from1 = ET.SubElement(transition, 'from')
to = ET.SubElement(transition, 'to')
dirList = glob.glob(path)
while len(background.findall("./static/file")) <= len([name for name in os.listdir('.') if os.path.isfile(name)]):
background.append(static)
background.append(transition)
continue
for fname in dirList:
to.text = fname
files.text = fname
from1.text = fname
print prettify(background)
This code outputs a correctly formatted xml but only with the last path, repeating it as many times as the number of files in folder. What I want is for it to print one path per "file" element and that same path on the preceeding "to" element and the next from "element". Maybe it is a simple solution that I'm not aware of. I'm still a newbie.
Thanks in advance