2

I have the following XML file which is longer but I only pick out the part that I want to change. I like to change the text from 'false' to 'true' and write back to the same file name. I've searched and read a few threads and the closest I get is the code below, but it does not do as what I want.

Here is the file:

<settings>
    <setting id="HomeMenuNoWeatherButton" type="bool">false</setting>
    <setting id="HomeMenuNoPicturesButton" type="bool">false</setting>
    <setting id="HomeMenuNoMusicButton" type="bool">false</setting>
</settings>

The code I've tried from one of the threads:

from xml.etree import ElementTree as et
tree = et.parse('path to settings.xml')
tree.find('settings/setting id="HomeMenuNoWeatherButton" type="bool"').text = 'true'
tree.find('settings/setting id="HomeMenuNoPicturesButton" type="bool"').text = 'true'
tree.find('settings/setting id="HomeMenuNoMusicButton" type="bool"').text = 'true'
tree.write('path to settings.xml')

I want the file to be:

<settings>
    <setting id="HomeMenuNoWeatherButton" type="bool">true</setting>
    <setting id="HomeMenuNoPicturesButton" type="bool">true</setting>
    <setting id="HomeMenuNoMusicButton" type="bool">true</setting>
</settings>

2 Answers 2

2

Your selector expression isn't a valid XPath expression. Use [] for filter aka predicate and use @ for attribute, for example:

tree = et.parse('path to settings.xml')
root = tree.getroot()
xpath = 'setting[@id="HomeMenuNoWeatherButton" and @type="bool"]'
root.find(xpath).text = 'true'
Sign up to request clarification or add additional context in comments.

Comments

-1

Instead of using:

tree.find('settings/setting id="HomeMenuNoWeatherButton" type="bool"').text = 'true'

Use:

tree.find('settings/setting id="HomeMenuNoWeatherButton" type="bool"').text.set('true')

2 Comments

I got error message(shorten message to match character count): Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> tree.find('settings/setting id="HomeMenuNoWeatherButton" type="bool"').text.set('false') .............. .............. File "C:\Python27\lib\xml\etree\ElementPath.py", line 285, in find return iterfind(elem, path, namespaces).next() File "C:\Python27\lib\xml\etree\ElementPath.py", line 263, in iterfind selector.append(ops[token[0]](next, token)) KeyError: '='
So that means you are getting an error on the formatting on the string you put in the find field : ('settings/setting id="HomeMenuNoWeatherButton" type="bool"'). From reading the documentation it looks like you have to do the find part differently. You can iterate through the root element and change the ones that you want to change like this: for setting in root.iter("setting"): setting.text = true

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.