OnionSandwich
OnionSandwich

Reputation: 237

In Python using etree how do I retain the doc type and declaration

I have an xml file that I'm adding to daily. The problem is when I read the file in then save it loses the xml declaration at the top of the file. The code I'm using:

def parseXML():

    xmlFile = open(myfile,'r')
    parser = etree.XMLParser(strip_cdata=False)
    tree = etree.parse(xmlFile, parser)
    return tree

xml_curr = parseXML()
xml_root = xml_curr.getroot()

I then append to this:

xml_root.append(new_elements)

Apologies if this doesn't quite make sense, python is new to me.

Upvotes: 0

Views: 183

Answers (1)

Pandian Muninathan
Pandian Muninathan

Reputation: 55

etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone="yes")

Upvotes: 1

Related Questions