Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding new XML node and Pretty printing XML in python
    primarykey
    data
    text
    <p>I can add the XML node using the ElementTree, but this returns the output in one single line instead of a tree structure look alike when I open the xml file in text format. I also tried using the minidom.toprettyxml but I do not know how to add the output to original XML. Since I would like the script to be reproducible in other environments, I prefer not using external libraries such as lxml. Can someone please help how I can pretty print the output? - python 2.7</p> <p>The Sample XML. This is how it looks both in text format and Explorer.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;default_locators &gt; &lt;locator_ref&gt; &lt;name&gt;cherry&lt;/name&gt; &lt;display_name&gt;cherrycherry&lt;/display_name&gt; &lt;workspace_properties&gt; &lt;factory_progid&gt;Workspace&lt;/factory_progid&gt; &lt;path&gt;InstallDir&lt;/path&gt; &lt;/workspace_properties&gt; &lt;/locator_ref&gt; &lt;/default_locators&gt; </code></pre> <p>Expected Output in both text format and Explorer.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;default_locators &gt; &lt;locator_ref&gt; &lt;name&gt;cherry&lt;/name&gt; &lt;display_name&gt;cherrycherry&lt;/display_name&gt; &lt;workspace_properties&gt; &lt;factory_progid&gt;Workspace&lt;/factory_progid&gt; &lt;path&gt;InstallDir&lt;/path&gt; &lt;/workspace_properties&gt; &lt;/locator_ref&gt; &lt;locator_ref&gt; &lt;name&gt;berry&lt;/name&gt; &lt;display_name&gt;berryberry&lt;/display_name&gt; &lt;workspace_properties&gt; &lt;factory_progid&gt;Workspace&lt;/factory_progid&gt; &lt;path&gt;C:\temp\temp&lt;/path&gt; &lt;/workspace_properties&gt; &lt;/locator_ref&gt; &lt;/default_locators&gt; </code></pre> <p>My script</p> <pre><code>#coding: cp932 import xml.etree.ElementTree as ET tree = ET.parse(r"C:\DefaultLocators.xml") root = tree.getroot() locator_ref = ET.SubElement(root, "locator_ref") name = ET.SubElement(locator_ref, "name") name.text = " berry" display_name = ET.SubElement(locator_ref, "display_name") display_name.text = "berryberry" workspace_properties = ET.SubElement(locator_ref, "workspace_properties") factory_progid = ET.SubElement(workspace_properties,"factory_progid") factory_progid.text = "Workspace" path = ET.SubElement(workspace_properties, "path") path.text = r"c:\temp\temp" tree.write(r"C:\DefaultLocators.xml", encoding='utf-8') </code></pre> <p>Returned output. After running my script, new nodes are added to my sample.xml file, but it returns output in one single line, with all newlines and indents removed from the original sample.xml file. At least thats how it looks when I open the sample.xml file in text format. However, When I open the sample.xml file in Explorer, it looks fine. I still see the newlines and indents as they were before. How can I keep the original tree structure in text format even after running the script?</p> <pre><code>&lt;default_locators&gt; &lt;locator_ref&gt; &lt;name&gt;cherry&lt;/name&gt; &lt;display_name&gt;cherrycherry&lt;/display_name&gt; &lt;workspace_properties&gt; &lt;factory_progid&gt;Workspace&lt;/factory_progid&gt; &lt;path&gt;InstallDir&lt;/path&gt; &lt;/workspace_properties&gt; &lt;/locator_ref&gt; &lt;locator_ref&gt;&lt;name&gt; berry&lt;/name&gt;&lt;display_name&gt;berryberry&lt;/display_name&gt;&lt;workspace_properties&gt;&lt;factory_progid&gt;Workspace&lt;/factory_progid&gt;&lt;path&gt;c:\temp\temp&lt;/path&gt;&lt;/workspace_properties&gt;&lt;/locator_ref&gt;&lt;/default_locators&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload