Python - search and replace content in XML file -


i have requirement have sample xml file defined structure. want search of tag/child in xml file, replace text/value input file , save changes new xml file preferably without effecting input sample xml.

i understand can achieved xml parser traverse through child intended. problem @ top of xml file have

<nc:data xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"> 

i have written python function shown below failing intended thing.

def search_replace():     replicate()        // function makes tmp file sample input xml work on.      tree = et.parse("/path/to/file/tmp_file.xml")     tree.find('nc:data/child1/child2/child3').text = 'test'     tree.write("new_file.xml") 

please suggest best approach handle this. not python-skilled of !!

you need create dictionary map prefix namespace uri , pass additional parameter of find() :

def search_replace():     replicate()     tree = et.parse("/path/to/file/tmp_file.xml")     nsmap = {'nc': 'urn:ietf:params:xml:ns:netconf:base:1.0'}     tree.find('nc:data/child1/child2/child3', nsmap).text = 'test'     tree.write("new_file.xml") 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -