title: python generating xml
body: <p>i am generating an XML template for zabbix using python minidom</p>
  
  <pre><code>#!/usr/bin/env python
  
  from xml.etree import ElementTree
  from xml.dom import minidom
  #from xml.etree.cElementTree import ElementTree
  from xml.etree.ElementTree import Element, SubElement, Comment
  import sys
  
  def prettify(elem):
      """returns a xml in pretty format"""
      orig_string = ElementTree.tostring(elem, 'utf-8')
      reparsed = minidom.parseString(orig_string)
      return reparsed.toprettyxml(indent="    ")
  
  def createXml():
      zabbix_export = Element('zabbix_export',{'version':'1.0','date':'07/03/2012','time':'18:40'})
      #zabbix_export = Element('zabbix_export')
      comment = Comment('Generated in python')
      zabbix_export.append(comment)
  
      hosts = SubElement(zabbix_export,'hosts')
  
      host = SubElement(hosts, 'host', {'name':'Template_KSHK_Stats'})
      host.text = 'this child contains text'
  
      proxy_hostid = SubElement(host, 'proxy_hostid')
      proxy_hostid.text = 'this is the subchild'
  
      useip = SubElement(host, 'useip')
      useip.text = '0'
  
      ip = SubElement(host, 'ip')
      ip.text = '0.0.0.0'
  
      port = SubElement(host, 'port')
      port.text = '0'
  
      print prettify(zabbix_export)
  
  def main():
      createXml()
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>This is the output i get:</p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt;
  &lt;zabbix_export date="07/03/2012" time="18:40" version="1.0"&gt;
      &lt;!-- Generated in python --&gt;
      &lt;hosts&gt;
          &lt;host name="Template_KSHK_Stats"&gt;
              this child contains text
              &lt;proxy_hostid&gt;
                  this is the subchild
              &lt;/proxy_hostid&gt;
              &lt;useip&gt;
                  0
              &lt;/useip&gt;
              &lt;ip&gt;
                  0.0.0.0
              &lt;/ip&gt;
              &lt;port&gt;
                  0
              &lt;/port&gt;
              &lt;status&gt;
                  3
              &lt;/status&gt;
  </code></pre>
  
  <p>i would like the subelements in the host tree to be in this format, </p>
  
  <pre><code>proxy_hostid&gt;this is the subchild&lt;/proxy_hostid&gt;
  &lt;useip&gt;0&lt;/useip&gt;
  &lt;ip&gt;0.0.0.0&lt;/ip&gt;
  &lt;port&gt;0&lt;/port&gt;
  &lt;status&gt;3&lt;/status&gt;
  </code></pre>
  
  <p>but because of the prettifyhack from <a href="http://www.doughellmann.com/PyMOTW/xml/etree/ElementTree/create.html" rel="nofollow">http://www.doughellmann.com/PyMOTW/xml/etree/ElementTree/create.html</a></p>
  
  <p>zabbix does not read the subelements properly..</p>
  
  <p>any idea how i can fix this??</p>
  
  <p><strong>FIXED:</strong></p>
  
  <p>using</p>
  
  <pre><code>def prettify(elem):
      """returns a xml in pretty format"""
      orig_string = ElementTree.tostring(elem, 'utf-8')
      reparsed = minidom.parseString(orig_string)
      uglyXml = reparsed.toprettyxml(indent="  ")
      text_re = re.compile('&gt;\n\s+([^&lt;&gt;\s].*?)\n\s+&lt;/', re.DOTALL)
      prettyXml = text_re.sub('&gt;\g&lt;1&gt;&lt;/', uglyXml)
      return prettyXml
  </code></pre>