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>This would help you</p>

<pre><code>import xml.etree.ElementTree as ET

tree = ET.parse('test.xml') # Path to input file
root = tree.getroot()

for prop in root.iter('.//*[@class="SvgMarker"]/prop'):
   prop.set('v', str(float(prop.get('v')) * 1.5))

tree.write('out.xml', encoding="UTF-8")
</code></pre>

<p>Ref: <a href="https://docs.python.org/2/library/xml.etree.elementtree.html#example" rel="nofollow">https://docs.python.org/2/library/xml.etree.elementtree.html#example</a></p>

python xml