Python Changing value of XML
<p>Hello im using lxml to try and change the value of a specific xml element.  Here is my code.</p>

<pre><code>directory = '/Users/eeamesX/work/data/expert/EFTlogs/20160725/IT'
XMLParser = etree.XMLParser(remove_blank_text=True)
for f in os.listdir(directory):
    if f.endswith(".xml"):

        xmlfile = directory + '/' + f
        tree = etree.parse(xmlfile, parser=XMLParser)
        root = tree.getroot()
        hardwareRevisionNode = root.find(".//hardwareRevision")

        if hardwareRevisionNode.text == "5":
            print " "
            print hardwareRevisionNode.text
            str(hardwareRevisionNode.text) == "DVT3"
            print hardwareRevisionNode.text
</code></pre>

<p>I want to change the 5 to a DVT3  instead it just prints it out below as 5 and 5.  I referenced <a href="http://stackoverflow.com/questions/12728719/change-xml-using-python">Change xml using python</a> .  Unfortunately it doesnt work for me.</p>

<p>looks like you need assignment <code>=</code> not comparison <code>==</code> and the cast to string <code>str()</code> is unnecessary.  once you've assigned the value, you'll want to write the result back out to the file:</p>

<pre><code>hardwareRevisionNode.text = "DVT3"

outfile = open(xmlfile, 'w')
oufile.write(etree.tostring(tree))
outfile.close()
</code></pre>

<p>good luck!</p>

python xml lxml