0

I am adding new elements (tags) to an existing XML file using python. The problem is that:

Appended XMl file is not printing each element on a new line (no whitespace); it is not pretty printed.

The output looks like this:

<queue flag="0x400004" node="parameter"><Enable Flag="0x400010" Id="167" node="parameter">true</Enable><Status Flag="0x200004" Id="168" node="parameter">Enabled</Status></queue><queue flag="0x400002" node="parameter"><Enable Flag="0x400010" Id="168" node="parameter">true</Enable><Status Flag="0x200004" Id="168" node="parameter">Enabled</Status></queue>

I want the output to be pretty printed so that each tag is on a separate line.

The python code I used for appending the xml file is:

import xml.dom.minidom as m

qu=dom.createElement("Queue")        #creating elements to be appended
qu.setAttribute("node","parameter")
qu.setAttribute("Id","166")
qu.setAttribute("Flag","0x400004")

child=dom.createElement("Enable")      #a sample child element "Enable" inside "queue"
child.setAttribute("node","object")
child.setAttribute("Id","167")
child.setAttribute("Flag","0x400010")

qu.appendChild(child)             #child appended to "queue"
root.appendChild(qu)        #appending "queue" to "root" tag of existing xml file

dom.writexml(open("existing_xml_file.xml","w"))             #to append the existing xml file

What changes do I need to make to my code to get a pretty printed output?

Thank you in advance...!!!

Prashu Pratik
  • 41
  • 1
  • 2
  • 6
  • http://stackoverflow.com/questions/749796/pretty-printing-xml-in-python – Jean-François Fabre Feb 27 '17 at 16:55
  • Straight from the documentation: `Node.writexml(writer, indent="", addindent="", newl="")`, The optional keyword parameters indent, addindent, and newl were added to support pretty output – gold_cy Feb 27 '17 at 16:55
  • @DmitryPolonskiy I am not sure how to use this command in my code. Can you please write an equivalent to work in my code?..... What exactly do I need to give values of "newl","indent" to get a new line after every closing tag..? – Prashu Pratik Feb 27 '17 at 17:55
  • @Jean-FrançoisFabre Yes I had seen that question too...although I am not very versed with xml.dom, so I am not sure how to use that toprettyprint() statement in my code..I tried writing it as it is in my code: "pretty_xml_as_string=dom.toprettyxml()" ...but this doesnot seem to work in my case...is there any changes I need to do make it work??? – Prashu Pratik Feb 27 '17 at 17:57
  • try this: `dom.writexml(open("existing_xml_file.xml","w"), indent = '\n')` – gold_cy Feb 27 '17 at 18:01
  • @DmitryPolonskiy This is producing unnecessary new lines to the existing elements of the used xml file. I just want to create new lines to elements that I am appending to the existing XML file...... can that be done somehow? – Prashu Pratik Feb 27 '17 at 18:05
  • Try to use the `addindent` and `newl` args. You have the script in front of you. Play around with these options. – gold_cy Feb 27 '17 at 18:13
  • @DmitryPolonskiy Also, this is creating new lines for every start tag and end tag. The output is like this: true I would have desired to get output having new line indent only after every closing tag of each element... – Prashu Pratik Feb 27 '17 at 18:14
  • @DmitryPolonskiy tried putting '\n' in all three separately... nothing works as desired. xmlwrite statement is causing the effect of indent on the whole existing xml file.... is there any way to induce pretty-print while a new element is created?.. that way only the new elements will be indented..... – Prashu Pratik Feb 27 '17 at 18:18

0 Answers0