1

I have made a sample XML file like this:

from lxml import etree
import xml.etree.ElementTree as et
root = etree.Element("{" + link + "}linkbase", attrib={"{" + xsi + "}schemaLocation" : schemaLocation}, nsmap=ns)
ch1_lvl1 = etree.SubElement(root, "{" + link + "}referenceLink", attrib={"{" + xlink + "}type" : elem_type[0], "{" + xlink + "}role" : role_prefix + role[0]})
ch1_lvl2 = etree.SubElement(ch1_lvl1, "{" + link + "}loc", attrib={"{" + xlink + "}type" : elem_type[1], "{" + xlink + "}href" : ch1_lvl2_href, "{" + xlink + "}label" : ch1_lvl2_label})
ch2_lvl2 = etree.SubElement(ch1_lvl1, "{" + link + "}reference", attrib={"{" + xlink + "}type" : elem_type[2], "{" + xlink + "}label" : ch2_lvl2_label, "{" + xlink + "}role" : role_prefix + role[1], "{" + xlink + "}id" : ch2_lvl2_id} )
ch1_lvl3 = etree.SubElement(ch2_lvl2, "{" + in_rbi_rep_par + "}Circular").text="DBS.No.FBC.BC.34/13.12.001/99-2000  dt April 6, 2000"
print(etree.tostring(root, pretty_print=True).decode("utf-8"))
r = etree.tostring(root).decode("utf-8")
file = open('s.txt','w')
file.write(r)

I am not including the definition of ns, schemaLocation and other variables as I don't see them relevant in the questions' context. The above code generates a file having only the Circular tag data i.e. DBS.No.FBC.BC.34/13.12.001/99-2000 dt April 6, 2000. However, I want the entire XML should be generated like so:

<link:linkbase xmlns:link="http://www.xbrl.org/2003/linkbase" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:in-ghi-rep-par="http://www.ghi.org/in-ghi-rep-par" xmlns:ref="http://www.xbrl.org/2006/ref" xmlns:xbrli="http://www.xbrl.org/2003/instance" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.xbrl.org/2006/ref http://www.xbrl.org/2006/ref-2006-02-27.xsd http://www.ghi.org/in-ghi-rep-par &#10;../core/in-ghi-rep-par.xsd http://www.xbrl.org/2003/linkbase http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd">
  <link:referenceLink xlink:type="extended" xlink:role="http://www.xbrl.org/2003/role/link">
    <link:loc xlink:type="locator" xlink:href="../core/in-ghi-rep.xsd#in-ghi-rep_ReportingPeriodTable" xlink:label="in-ghi-rep_ReportingPeriodTable"/>
    <link:reference xlink:type="resource" xlink:label="res_1" xlink:role="http://www.xbrl.org/2003/role/disclosureRef" xlink:id="res_1">
      <in-ghi-rep-par:Circular>DBS.No.FBC.BC.34/13.12.001/99-2000  dt April 6, 2000</in-ghi-rep-par:Circular>
    </link:reference>
  </link:referenceLink>
</link:linkbase>

The above text is generated but when I write to a file, it just writes the Circular tag. I am unable to find what I am doing wrong here. Any help?

2
  • Does this answer your question? Write xml file using lxml library in Python Commented Mar 30, 2020 at 8:02
  • Nopes, it gives the same output i.e. only the Circular tag is written to the file. Commented Mar 30, 2020 at 8:04

1 Answer 1

0

Based on the docs tree.write() is what you want. In your case, I think, root.write("s.txt", encoding="utf-8") should do (I didn't test it).

Sign up to request clarification or add additional context in comments.

2 Comments

et = etree.ElementTree(root) with open('samp.txt', 'wb') as f: et.write(f, encoding="utf-8", xml_declaration=True, pretty_print=True). This did it. Thanks
The answer is not the exact answer. I got the idea though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.