I am trying to read, modify, and write an XML file with lxml 4.1.1 in Python 2.7.6.
My code:
import lxml.etree as et
fn_xml_in = 'in.xml'
parser = et.XMLParser(remove_blank_text=True)
xml_doc = et.parse(fn_xml_in, parser)
xml_doc.getroot().find('b').append(et.Element('c'))
xml_doc.write('out.xml', method='html', pretty_print=True)
The input file in.xml
looks like this:
<a>
<b/>
</a>
And the produced output file out.xml
:
<a>
<b><c></c></b>
</a>
Or when I set remove_blank_text=True
:
<a><b><c></c></b></a>
I would have expected lxml to insert line breaks and indentation within the b
element:
<a>
<b>
<c></c>
</b>
</a>
How can I achieve this?
I have tried some tidy
lib wrappers, but they seem to specialize on HTML rather than XML.
I have also tried to add newline characters as b
's tail
, but then even the indentation is broken.
Edit: I need the c
element to remain separated in an opening and a closing tag: <c></c>
. This is why I use method='HTML'
in the example.
Use the "xml" output method when writing (that's the default so it does not have to be given explicitly).
Set the text
property of the c
element to an empty string to ensure that the element gets serialized as <c></c>
.
Code:
import lxml.etree as et
parser = et.XMLParser(remove_blank_text=True)
xml_doc = et.parse('in.xml', parser)
b = xml_doc.getroot().find('b')
c = et.Element('c')
c.text=''
b.append(c)
xml_doc.write('out.xml', pretty_print=True)
Result (out.xml):
<a>
<b>
<c></c>
</b>
</a>