Adding new line and encoding infomation while creating XML/XBRL with ElementTree/Python

Posted on Tue 10 July 2012 in XBRL

I am making xbrl files with python ElementTree/python module. But there is a problem while creating an xbrl file. It writes it into a single line, which creates confusion a lot.

So i have tried to modified the native ElementTree class which writes a xml  part. In windows it is located at the "C:\Python27\Lib\xml\etree\ElementTree.py" . In this file there is a function named as "_serialize_xml";  and i have hacked it a little bit. I have added a "\n" where angle bracket of xml element is defined. Screenshot for that is given below._serialize_xml

So i got my output xbrl as follows.

xbrl
outputHere you can see that now my python script makes xbrl package as i have wanted from it.
Anyway, rest is i want to add encoding information to it, how?

Below is the solution,

    >>>ins_out=open(instant_filename,'wb',1000)

    >>>ElementTree.ElementTree(top).write(ins_out,encoding="ASCII")
    >>>ins_out.close()

here "instant_filename" is the output filename (example; "htwr-20121231.xml") while  "top" is the root element below which i have created my xml/xbrl file. Just add value of encoding to the instance method "write" of ElementTree.ElementTree