[Python] Python: Access the next element in XML using ElementTree

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    My XML file has no tags, which makes parsing not so friendly. I'm trying to find an element and change it:

    ...
    <dict>
    <key>Arguments</key>
    <dict>
    <key>TestArchive</key
    <string>testArchive.tar</string>
    </dict>
    ....
    </dict>
    ...


    What I'm trying to do is to get and change "testArchive.tar" string, because this string may change every time, I use TestArchive as an anchor

    from xml.etree import ElementTree
    with open ('file.xml', 'rt') as f:
    tree = ElementTree.parse(f)

    for item in tree.iter():
    if item.text == "TestArchive":
    ....


    How can I get the next element? Which in my case supposed to be the item I'm looking for?

    Continue reading...

Compartilhe esta Página