-1

I need to write a Python script that reads and replaces some data in an XML file. The data that is replaced has to be read automatically from a directory (it's a file's name)

<setting name="abc" serializeAs="String">
<value>fw.version.1.1</value>

the fw.version1.1 has to be replaced with the file name from a folder.

Could use some help:)

thanks, Robert

1
  • 1
    OK, then write one. Do you have a question? Commented Apr 26, 2016 at 8:47

1 Answer 1

0

Assuming the XML File looks something like that test.xml:

<someXml>
<setting name="abc" serializeAs="String"/>
<value>fw.version.1.1</value>
</someXml>

To read the XML Data from File:

from lxml import etree
parser = etree.XMLParser(remove_blank_text=True)
xmlData = etree.parse('test.xml', parser )

Reading the text from the value Tag:

xmlData.xpath('//value')[0].text

Writing new text to the value Tag:

xmlData.xpath('//value')[0].text = "test"

And finally write your changes to the same (or any other) File:

xmlData.write( 'test.xml', pretty_print=True )
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.