I am trying to add a subitem to an existing XML file. I can add the element but its added to an existing element line and not on a new line.
I have tried using the toprettyxml()
function but this just doubles the line spacing
Here is XML being read
<MainItem config="mainItem">
<DisplayName name="" />
<SubItems>
<SubItem path="path01" />
<SubItem path="path02" />
<SubItem path="path03" />
</SubItems>
</MainItem>
My current code
from xml.dom.minidom import *
dom = parse(r"path/myfile")
element = dom.createElement("SubItem")
element.appendChild(dom.createTextNode("NewPath03"))
cd = dom.getElementsByTagName("SubItem")[2]
cd.parentNode.insertBefore(element, cd)
Here is result
<MainItem config="mainItem">
<DisplayName name=""/>
<SubItems>
<SubItem path="path01"/>
<SubItem path="path02"/>
<SubItem>NewPath03</SubItem><SubItem path="path03"/>
</SubItems>
</MainItem>
The expected result
<MainItem config="mainItem">
<DisplayName name="" />
<SubItems>
<SubItem path="path01" />
<SubItem path="path02" />
<SubItem path="path03" />
<SubItem path="path04" />
</SubItems>
</MainItem>