0

In my code, I am changing an existed formatted string in XML with predefined format with ElementTree in Python.

<Value xsi:type='xs:string'>{&quot;name&quot;:&quot;Test123&quot;,&quot;type&quot;:&quot;}</Value>

New text adding by: ValueNode.text = '{&quot;name&quot;:&quot;NewTextdemo&quot;}'

and to save the file I am using doc.write(path_to_XML_file)

The problem is, that the doc.write(path_to_XML_file) is changing the &quot; to &amp;quot; further entity name - and so the result XML is invalid.

Does anybody know how to avoid it? How to set write function to omits conding these special characters?

Thank you

mzjn
  • 48,958
  • 13
  • 128
  • 248
VilemRousi
  • 2,082
  • 4
  • 24
  • 34
  • Similar to https://stackoverflow.com/q/7986272/407651 – mzjn Apr 04 '20 at 06:36
  • I would not expect `"` to be changed to `&quot;`, since `quot` is a predefined entity in XML. `manifestName` on the other hand (see the linked question) is not a predefined entity. – mzjn Apr 04 '20 at 07:00
  • Dear @mzjn, I wouldn´t expext it either, because characer " is not converted by wirte function, so I don´t know why & is. The problem is not when assining new string to variable - there it works fine. The problem is with write() function. This function breaks the string. – VilemRousi Apr 04 '20 at 08:32
  • 1
    `&` is replaced with `&` in this function in ElementTree.py: https://github.com/python/cpython/blob/master/Lib/xml/etree/ElementTree.py#L1033. – mzjn Apr 04 '20 at 14:21
  • Dear @mzjn thank you. So it seems it's by design :-( – VilemRousi Apr 04 '20 at 19:50

0 Answers0