1

I'm trying to create an xml file mimicing another applications xml file. I can't figure out how to write the following line to xml...

<![CDATA[execfile("C:\users\me\run.py")]]>

it always wants to convert it to

&lt;![CDATA[execfile(&quot;C:\users\me\run.py&quot;)]]&gt;

The complete output currently looks like this.

<?xml version="1.0" encoding="UTF-8"?>
<shelfDocument>
    <tool name="MyTool" label="MyTool">
        <script scriptType="python">&lt;![CDATA[execfile(&quot;C:\users\me\run.py&quot;)]]&gt;</script>
    </tool>
</shelfDocument>

I need it to look like this

<?xml version="1.0" encoding="UTF-8"?>
<shelfDocument>
    <tool name="MyTool" label="MyTool">
        <script scriptType="python">
            <![CDATA[execfile("C:\users\me\run.py")]]>
        </script>
    </tool>
</shelfDocument>

here is my python code...

import os
import sys
import xml.dom.minidom
import xml.etree.ElementTree as ET

root = ET.Element('shelfDocument')
scriptItemEl = ET.SubElement(root, 'tool', name='MyTool', label='MyTool')

cmd = '<![CDATA[execfile("{}")]]>'.format('C:\\users\\me\\run.py')
scriptEl = ET.SubElement(scriptItemEl, 'script', scriptType='python')
scriptEl.text = '{}'.format(cmd)

xmlstr = ET.tostring(root, encoding='UTF-8', method="xml")
xmlObj = xml.dom.minidom.parseString(xmlstr)
xmlstr = xmlObj.toprettyxml(encoding='UTF-8')

filepath = 'output.xml'
with open(filepath, 'wb') as output:
    output.write(xmlstr)

mzjn
  • 48,958
  • 13
  • 128
  • 248
JokerMartini
  • 5,674
  • 9
  • 83
  • 193

0 Answers0