SoftwareDveloper
SoftwareDveloper

Reputation: 742

Python: Write XML Contents to a file using lxml library

I have a Python function that writes xml string to a file. The code is as follows:

My file_contents is a string as follows:

<A>Some Text</A>
<B>Some other Text</B>

I would like to enclose this string inside and prepend the xml header : to the file. I can't seem to figure this out.

from lxml import etree

def create_or_overwrite_file(file_name, file_contents):
    try:        
        print('file contents to be written: %s' % file_contents)

        '''
        #root = etree.fromstring(file_contents) #this line definitely fails because file_contents is NOT a valid XML

        #print('root: %s' % str(root))
        #newroot = etree.Element('RootElement')
        #newroot.insert(0, root)
        #print('newroot: %s' % newroot)
        #new_file_contents = etree.tostring(newroot)
        #print('NewFileContents: %s' % str(new_file_contents))
         '''

        root = etree.fromstring(file_contents).getroot()
        et = etree.ElementTree(root)
        with open(str(file_name), "wb") as f:
            et.write(f, encoding="utf-8", xml_declaration=True, pretty_print=True)
            print('wrote file_contents to %s' % str(file_name))
    except Exception as f:
        print(str(f))

I can't seem to get this code working. Any help is appreciated.

Upvotes: 0

Views: 994

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54698

If you are parsing from a string (as opposed to reading from a file). then fromstring already returns the root. You do not need the .getroot() call:

    root = etree.fromstring(file_contents)

With that change, your code works for me.

Now, it's not clear why you are doing this. If file_contents is already XML, as it must be for this to work, then why not just open(file_name).write(file_contents)? Why decode the XML and then re-encode it again?


Followup

If all you want is a wrapper, then this would work:

def create_or_overwrite_file(file_name, file_contents):
    f = open(file_name,'w')
    print( '<?xml version="1.0"?>\n<root>', file=f )
    f.write(file_contents)
    print( '</root>', file=f )

It's probably more efficient not to involve lxml. Where it makes more sense is up to you.

Upvotes: 1

Related Questions