1

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

3 Answers 3

3

You are just creating a single element and adding this same element multiple times.

In your for loop, you are assigning the members of that element over and over, so finally it ends up with just the last fname in there

You need to create a fresh element and populate it each time in the for loop

Probably you should have something more like this

background = ET.Element('background')
dirList = glob.glob(path)

for fname in dirList:   

    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')

    to.text = fname
    files.text = fname
    from1.text = fname
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your quick answer, but your solution didn't solve my question completely...I have to take out the two "background.append" functions becaus they were making extra copies of those branches.
@Unkuiri, ok I jsut copied those from your question. I've taken them out to reduce confusion
0

You are stomping over to/files/from1 on each iteration without saving them anywhere. Each iteration overwrites whats from the previous iteration before you have a chance to do anything with the data.

Did you mean to store these variables away? Did you mean to do print prettify(background) on each iteration?

Comments

0

I've managed to solve this question with this code:

    import glob
    from xml.etree import ElementTree
    from xml.dom import minidom

    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="  ")

    import xml.etree.ElementTree as ET

    path = "/home/unkuiri/Ubuntu One/Wallpapers/*"


    background = ET.Element('background')
    dirList = glob.glob(path)
    starttime = ET.SubElement(background, 'starttime')
    year = ET.SubElement(starttime, 'year')
    year.text = '2012'
    month = ET.SubElement(starttime, 'month')
    month.text = '10'
    day = ET.SubElement(starttime, 'day')
    day.text = '10'
    hour = ET.SubElement(starttime, 'hour')
    hour.text = '00'
    minute = ET.SubElement(starttime, 'minute')
    minute.text = '00'
    second = ET.SubElement(starttime, 'second')
    second.text = '00'


    for i,fname in enumerate(dirList):    

        static = ET.SubElement(background, 'static')
        duration_stat = ET.SubElement(static, 'duration')
        duration_stat.text = '1795.0'
        files = ET.SubElement(static, 'file')
        transition = ET.SubElement(background, 'transition')
        duration_trans = ET.SubElement(transition, 'duration')
        duration_trans.text = '5.0'
        from1 = ET.SubElement(transition, 'from')
        to = ET.SubElement(transition, 'to')

        from1.text = dirList[i-1]

        files.text = dirList[i-1]

        to.text = dirList[i]


    print prettify(background)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.