1

I am current using ElementTree write a XML file

for i in range(len(lang_list)):
    body = ET.SubElement(root, "body")
    body.set("lang", lang_list[i])
    # body.text = text_list[i] + " " + common_text
    body.text =unescape(escape('<![CDATA[')) + text_list[i] + " " +     common_text + unescape(escape(']]>'))
    body.tail = "\n\t"

outpath = os.getcwd()
file = outpath + "\\test.xml"
tree.write(file, xml_declaration=True)

but there have a XML declaration, such as

DOCTYPE email-template PUBLIC "-//yourcompany, Inc.//DTD email-template//EN" "email-template.dtd">

How to write this information into the XML file?

Community
  • 1
  • 1
Shane Wang
  • 21
  • 1
  • 2
  • I wrote up an answer here but the question was closed before I submitted it. You can see my answer [here](https://gist.github.com/larsks/de0d32d3796609cd856da7b95e7e4808). – larsks Sep 01 '16 at 04:26

1 Answers1

1

One possible (pragmatic) solution is to replace your last line

tree.write(file, xml_declaration=True)

with

with open(file, 'wb') as f:
    f.write(b'<?xml version="1.0" encoding="UTF-8"?>');
    f.write(b'<!DOCTYPE email-template PUBLIC "-//yourcompany, Inc.//DTD email-template//EN" "email-template.dtd">')
    tree.write(f, xml_declaration=False, encoding='utf-8')  
DAXaholic
  • 33,312
  • 6
  • 76
  • 74