xml.etree.Elementree Python 3 解析器在 xml 中循环多个层时不起作用

问题描述 投票:0回答:1

美化后的数据串:

<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代码:

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 tekker_grounding.tag == "Locks":# and tekker_name == parent:
if not tekker_name == parent:
if not tekker_grounding.attrib["Name"] == params["selected_tekker_name"]
属性的设置符合我的预期。

但是,一旦循环完成循环,并且使用 tostring 方法将数据保存回来:

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

不包含新设置的属性,所有数据保持不变。

我希望能够设置并保存这些属性。

请问有什么想法吗?

python-3.x xml elementtree
1个回答
0
投票

毫不奇怪我没有得到任何人的答复。有用。令人烦恼的是,正在输入数据的 UI 没有获取正确的数据。抱歉浪费了您的时间。

© www.soinside.com 2019 - 2024. All rights reserved.