3

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Martijn! It worked and got me much closer to a solution. I'm working on a Windows platform and have looked at lxml, but it seems Windows is a bit of a pain to configure lxml properly. I will definitely take a look at lxml once I have more time. For the time being, your solution works fine! Thanks again.

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.