Reputation: 75
Because the XML file I'm dealing with has inconsistent subelements I'm having some difficulty finding and editing a specific subelement that doesn't appear in the series of elements until further down the file. I'm using Python's ElementTree included with Python 2.7.1.
Here's an example of the type of XML file I'm dealing with:
<?xml vin="1.0" encoding="UTF-8" standalone="yes"?>
<whatever id='Subaru' YouCanDriveMyCar='Wednesday' transmission='stick'>
<model id='Ford' year='1972'>A</model>
<model id='Chevrolet' vin="1234567890" stereo='Alpine' airconditioning='notworking'>Volt</model>
<model id='Dodge' vin="3456789012" airconditioning='working'>Durango</model>
<model id='Mercedes' vin="4567890123" airconditioning='none'>S150</model>
<model id='BMW'>M350</model>
<model id='Volkswagen' vin="5678901234" stereo='Sony'>Beetle</model>
<model id='Honda' vin="6789012345" airconditioning="blowsicecubes">Accord</model>
</whatever>
In this example, I want to locate model id='Volkswagen' and change stereo='Sony' to 'Blaupunkt'.
If I use ElementTree to search for attribute 'stereo' it errors out because 'stereo' isn't contained in the elements at the top of the file.
Any help or hints would be greatly appreciated.
Upvotes: 3
Views: 4289
Reputation: 1122352
Use a simple XPath expression to find the right model:
volkswagen = tree.find('.//model[@id="Volkswagen"]')
then simply adjust the stereo
attribute:
volkswagen.set('stereo', 'Blaupunkt')
You could also search for model
elements with a stereo
attribute:
models_with_stereos = tree.findall('.//model[@stereo]')
then filter further on those elements.
If you switch to using lxml
(an alternative implementation of the ElementTree API) you can use more complex XPath expressions still, including matching only model
elements with id="Volkswagen"
and a stereo
attribute.
Upvotes: 3