I’m very new to python and I need to modify the
<test name="test02"></xmpp> to <test name="test03"></xmpp>
<temp-config>QA</temp-config> to <temp-config>Prod</temp-config>
for all 5 occurrences using python.
Not sure what lib to use. Any help in this is really appreciated.
<config>
<logging></logging>
<test-mode>false</test-mode>
<test name="test02"></xmpp>
<mail></mail>
<test-system>0</test-system>
<system id="0" name="suite1" type="regression">
<temp-config>QA</temp-config>
<rpm>0.5</rpm>
<cycles>3</cycles>
</system>
<system id="1" name="suite2" type="regression">
<temp-config>QA</temp-config>
<rpm>0.5</rpm>
<cycles>3</cycles>
</system>
<system id="2" name="suite3" type="regression">
<temp-config>QA</temp-config>
<rpm>0.5</rpm>
<cycles>3</cycles>
</system>
<system id="3" name="suite4" type="regression">
<temp-config>QA</temp-config>
<rpm>0.5</rpm>
<cycles>3</cycles>
</system>
<system id="4" name="suite5" type="regression">
<temp-config>QA</temp-config>
<rpm>0.5</rpm>
<cycles>3</cycles>
</system>
</config>
Use lxml. This example uses
lxml.etreeand would actually fail on your example xml, as it has some unclosed tags in it. If you have the same problems with the real-world data that you are going to parse, uselxml.html,that can handle broken xml (instructions added to the code as comments).Note: As pointed out by others, you have some less powerful alternatives in the standard library. For simple tasks as this they may be well suited, however, if your xml files are broken, parsing them with standard library tools equals wasting your time.