Convert a long XML string into indented format

L

Laguna

Hi,

I have an XML file in a single long string. How can I convert it into
the nicely indented format as displayed by Internet Explorer using
Python?

Thanks,
Laguna

Input XML file (one continuous string without newline):

<!-- $Revision: 12 $ --> <!-- $Date: 12/13/04 5:13p $ --> <control>
<schedule> <minute>*/2</minute> <hour>*</hour>
<day_of_month>*</day_of_month> <month>*</month>
<day_of_week>*</day_of_week> </schedule> </control>

Output XML file:

<!-- $Revision: 12 $ -->
<!-- $Date: 12/13/04 5:13p $ -->
<control>
<schedule>
<minute>*/2</minute>
<hour>*</hour>
<day_of_month>*</day_of_month>
<month>*</month>
<day_of_week>*</day_of_week>
</schedule>
</control>
 
L

Laguna

Excellent! This is exactly what I was looking for.

Thank you Jim!

My example code:

import xml.dom.ext
import xml.dom.minidom
doc = xml.dom.minidom.parse('myfile.xml')
xml.dom.ext.PrettyPrint(doc)
 
P

Paul Boddie

Jim said:
You want to pretty print. Have a look at
http://www.boddie.org.uk/python/XML_intro.html
for example.

Thank you to Paul, I've found that page useful,

Let me know if there's anything else that you think it should cover!
After updating the HTML parsing page and rediscovering some things (I'd
forgotten that PyXML supported HTML parsing, for example), it could
well be the case that the XML introduction needs some modernising, too.
Still, both documents are like reminders of forgotten Python library
functionality. ;-)

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top