Below code will parse the xml into a string, then replace
with space
and then write to a new xml file. You can do other processing in between, depending what exactly you want to do.
from lxml import etree
tree = etree.parse('some.xml')
root = tree.getroot()
# Get the whole XML content as string
xml_in_str = etree.tostring(root)
# Replace all
with space
new_xml_data = xml_in_str.replace(r'
', ' ')
# Do the processing with the new_xml_data string which is formatted
# Maybe also write to a new XML file, without the
with open('newxml.xml', 'w') as f:
f.write(new_xml_data)
some.xml
looks like:
<root>
<a> dsdfs
dsf
sdf
</a>
<bds>
<d>sdf
</d>
<d>sdf
sdf
sdf
</d>
</bds>
<bds>
<d>sdf
</d>
<d>sdf
sdf
sdf
</d>
</bds>
<bds>
<d>sdf
</d>
<d>sdf
sdf
sdf
</d>
</bds>
</root>
newxml.xml
looks like:
<root>
<a> dsdfs dsf sdf </a>
<bds>
<d>sdf </d>
<d>sdf sdf sdf </d>
</bds>
<bds>
<d>sdf </d>
<d>sdf sdf sdf </d>
</bds>
<bds>
<d>sdf </d>
<d>sdf sdf sdf </d>
</bds>
</root>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…