Search code examples
pythonxmlprettify

prettify adding extra lines in xml


I'm using Prettify to make my XML file readable. I am adding some new info in to an excising XML file but when i save it to a file i get extra lines in between the lines. is there a way of removing these line? Below is the code i'm using

import xml.etree.ElementTree as xml
import xml.dom.minidom as  minidom
from lxml import etree

def prettify(elem):
    rough_string = xml.tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="\t")


cid = "[123,123,123,123,123]"

doc = xml.parse('test.xml')
root = doc.getroot()

root.getchildren().index(root.find('card'))

e = xml.Element('card')
e.set('id', cid)
n = xml.SubElement(e, "name")
n.text = "FOLDER"
r = xml.SubElement(e, "red")
r.text = "FILE.AVI"
g = xml.SubElement(e, "green")
g.text = "FILE.AVI"
b = xml.SubElement(e, "blue")
b.text = "FILE.AVI"

root.insert(0, e)

doc2 =  prettify(root)

with open("testnew.xml", "w") as f:
    f.write(doc2)

Below is what i get in the file

<data>


    <card id="[123,123,123,123,123]">
            <name>FOLDER</name>
            <red>FILE.AVI</red>
            <green>FILE.AVI</green>
            <blue>FILE.AVI</blue>
    </card>
    <card id="[000,000,000,000,000]">


            <name>Colours</name>


            <red>/media/usb/cow.avi</red>


            <green>/media/usb/pig.avi</green>


            <blue>/media/usb/cat.avi</blue>


    </card>


</data>

input file "test.xml" looks like

<data>
   <card id="[000,000,000,000,000]">
        <name>Colours</name>
        <red>/media/usb/cow.avi</red>
        <green>/media/usb/pig.avi</green>
        <blue>/media/usb/cat.avi</blue>
   </card>
</data>

Solution

  • The new content added is being printed fine. Removing any "prettification" of the existing text solves the issue

    Add

    for elem in root.iter('*'):
        if elem == e:
            print "Added XML node does not need to be stripped"
            continue
        if elem.text is not None:
            elem.text = elem.text.strip()
        if elem.tail is not None:
            elem.tail = elem.tail.strip()
    

    before calling

     doc2 =  prettify(root)
    

    Related answer: Python how to strip white-spaces from xml text nodes