python - use xml.etree.elementtree to write out nicely formatted xml files -


this question has answer here:

i trying use xml.etree.elementtree write out xml files python. issue keep getting generated in single line. want able reference them if possible able have written out cleanly.

this getting

    <language><en><port>port</port><username>username</username></en><ch><port>ip地址</port><username>用户名称</username></ch></language> 

this see.

    <language>         <en>             <port>port</port>             <username>username</username>         </en>         <ch>             <port>ip地址</port>             <username>用户名称</username>         </ch>     </language> 

thanks help!

you can use function toprettyxml xml.dom.minidom in order that:

def prettify(elem):     """return pretty-printed xml string element.     """     rough_string = elementtree.tostring(elem, 'utf-8')     reparsed = minidom.parsestring(rough_string)     return reparsed.toprettyxml(indent="\t") 

the idea print element in string, parse using minidom , convert again in xml using toprettyxml function.

source: http://pymotw.com/2/xml/etree/elementtree/create.html


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -