python XML find and replace
<p>After fighting a day with python / etree without considerable success:</p>

<p>I have a xml file (items.xml)</p>

<pre><code>&lt;symbols&gt;
    &lt;symbol&gt;
        &lt;layer class="SvgMarker"&gt;
            &lt;prop k="size" v="6.89"/&gt;
        &lt;/layer&gt;
    &lt;/symbol&gt;
    &lt;symbol&gt;
        &lt;layer class="SvgMarker"&gt;
            &lt;prop k="size" v="3.56"/&gt;
        &lt;/layer&gt;
    &lt;/symbol&gt;
    &lt;symbol&gt;
        &lt;layer class="line"&gt;
            &lt;prop k="size" v="1"/&gt;
        &lt;/layer&gt;
    &lt;/symbol&gt;            
&lt;/symbols&gt;
</code></pre>

<p><strong>Questions</strong></p>

<ol>
<li>read this file </li>
<li>find all prop elements which have a parent element namend "layer" with class "SvgMarker" </li>
<li>multiply the value of v with 1.5 </li>
<li>write the content back</li>
</ol>

<p>I do not stick on etree if there is something easier.</p>

<p>You need to take care of hierarchy in xml tags and their type conversion to perform multiplication. I tested below code with your xml, it works fine.</p>

<pre><code>import xml.etree.ElementTree as ET
tree = ET.parse('homemade.xml')                 #Step 1
root = tree.getroot()
for symbol in tree.findall('symbol'):
    for layer in symbol.findall('layer'):
        class_ = layer.get('class')
        if(class_=="SvgMarker"):                #Step 2
            for prop in layer.findall('prop'):
                new_v = prop.get('v')
                new_v = float(new_v)*1.5        #Step 3
                prop.set('v',str(new_v))
outFile = open('homemade.xml', 'w')
tree.write(outFile)                             #Step 4
</code></pre>

<p>Hope this helps.</p>

python xml