xml.etree.Elementree Python 3 parser doesn't work when looping through mulpile layers in the xml

72 Views Asked by At

Data string after beautifying:

<Root>
    <Examplerecords>
        <Examplerecord animal="Cat" mode="DOG" spec="fourlegged"birth="2010">      
            <Grounding Cakers="" DepthBox="0" Numbers="101,102,103,104,105" Name="Lioness"Statuatte="exist" Ripe="Yes" Queues="33,34,35">
                <Locks DepthBox="0" Numbers="101,102,103,104,105" Bringit="" Name="Dora" Ripe="layeroptext" Queues="33,34,35" />
            </Grounding>
        </Examplerecord>
    </Examplerecords>
</Root>

Python Code:

import xml.etree.ElementTree as etree

def set_tekker_data(self, params, parent):
    # ignore this:
    # if params["idx"] is None:
        # return False

    tekker_data = etree.fromstring(rootScene.exampleRecordBettings.bettings)
    if not tekker_data:
        return False

    example_records = tekker_data[self.Examplerecords][params["idx"]]

    tekker_name = ""
    for tekker_grounding in example_records:

        if tekker_grounding.tag == "Grounding":
            tekker_name = tekker_grounding.attrib["Name"]

            if tekker_name == params["selected_tekker_name"]:
                tekker_grounding.attrib["Name"] = params["name"]
                tekker_grounding.attrib["Ripe"] = params["ripe"]
                tekker_grounding.attrib["Cakers"] = params["cakers"]
                tekker_grounding.attrib["DepthBox"] = params["depthbox"]
                tekker_grounding.attrib["Numbers"] = params["numbers"]
                tekker_grounding.attrib["Queues"] = params["queues"]
                tekker_grounding.attrib["Statuatte"] = params["frames"]

        if tekker_grounding.tag == "Locks":# and tekker_name == parent:

            # if I were to set attribs here, it works, because it does apply
            # the changes to all "tekker_grounding.tag == "Locks" and
            # it gets saved back to the string

            if not tekker_name == parent:
                continue

            if not tekker_grounding.attrib["Name"] == params["selected_tekker_name"]:
                continue

            # if conditions are met, attributes are set, but it doesn't get
            # saved back tostring once we exit the loop
            # tried breaking out the loop - nothing changes
            # tried setting attribs in all possible ways - nothing changes

            tekker_name = tekker_grounding.attrib["Name"]

            tekker_grounding.set("Name", params["name"])
            tekker_grounding.set("Ripe", params["ripe"])
            tekker_grounding.set("DepthBox", params["depthbox"])
            tekker_grounding.set("Bringit", params["bringit"])
            tekker_grounding.set("Numbers", params["numbers"])
            tekker_grounding.set("Queues", params["queues"])

    rootScene.exampleRecordBettings.bettings = etree.tostring(
        tekker_data, encoding="unicode")
    
    return True

If the below conditions are met: if tekker_grounding.tag == "Locks":# and tekker_name == parent: if not tekker_name == parent: if not tekker_grounding.attrib["Name"] == params["selected_tekker_name"] the attributes are being set as I would expect.

However once the loop has finished looping, and data is being saved back with the tostring method:

rootScene.exampleRecordBettings.bettings = etree.tostring(
        tekker_data, encoding="unicode")

It doesn't contain the newly set attributes and all data remains the same.

I'd like to be able to set and save those attributes.

Any ideas, please?

0

There are 0 best solutions below