0

I try to do this thing here :

BEFORE

<a>
    <b>
        <c>
            <d>
                <name>VERSION</name>
                <description />
                <defaultValue>v1.0.0</defaultValue>
                <trim>false</trim>
            </d>
            <d>
                <name>LINK</name>
                <description />
                <defaultValue>current</defaultValue>
                <trim>false</trim>
            </d>
        </c>
    <b>
</a>

AFTER

<a>
    <b>
        <c>
            <d>
                <name>VERSION</name>
                <description />
                <defaultValue>v2.0.0</defaultValue>
                <trim>false</trim>
            </d>
            <d>
                <name>LINK</name>
                <description />
                <defaultValue>I changed the link</defaultValue>
                <trim>false</trim>
            </d>
        </c>
    <b>
</a>

I have an XML file and I want to change the default values. I tried with this code but didn't work and idk how to handle it :

from xml.etree import ElementTree as ET
tab = ["V2.0.0, "I changed the link"]
i = 0
tree = ET.parse("myfile.xml")
for child in tree.findall(".//d"):
    tree.find(".//defaultValue").text = tab[i]
    i=+1

Thank you for your help !

1 Answer 1

1

Try to change

tree.find(".//defaultValue").text = tab[i]

with

child.find(".//defaultValue").text = tab[i]

Add this line of code as the last line of your script in order to see the modified XML

ET.dump(tree)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your time, I was so close lol ... but it is working for me !! So thank you

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.