-1

I am trying to rewrite an XML file at a specific point.

I have a tag ['db']['usa']['user']['id']['number'] - for example...

I need to replace the value of the tag number. I opened the file

with open('myFile.xml', 'a') as file:
    myFile = xmltodict.parse(file.read())

and then I just say that

myFile['db']['usa']['user']['id']['number'] = 1

But it doesn't really change the value to 1

How could I open an xml file and rewrite a specific value to a tag?

Thanks

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
EAzevedo
  • 751
  • 2
  • 17
  • 46
  • Not necessarily a dup, but see this: https://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python?rq=1 – DeepSpace Oct 20 '17 at 09:49
  • I can parse the xml file, that is actually not where I got stuck. My problem is that after reading the file I can not say that the value of that tag is now 1 – EAzevedo Oct 20 '17 at 09:53
  • 1
    Why do you expect the file's content to change? you never write back to the file. – DeepSpace Oct 20 '17 at 11:13

1 Answers1

0

I assigned the node to a variable:

node = ['db']['usa']['user']['id']['number']

and when returning the file I wrote the variable where it should be:

def returnFile(node, encoded):

     return '''<ID='''+node+'''>
     <CONTENTTYPE>image/jpeg</CONTENTTYPE>
     <CONTENTS>'''+str(encoded)+'''</CONTENTS>
     </BINARY-INFO-AZRLICHTBILD>'''

That solved my problem

EAzevedo
  • 751
  • 2
  • 17
  • 46