473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Convert a long XML string into indented format

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>

Jan 25 '06 #1
3 9112
Jim
Laguna wrote:
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?

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,
Jim

Jan 25 '06 #2
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)

Jan 25 '06 #3
Jim wrote:

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

Jan 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kyle Blaney | last post by:
Is there a way to control how the "Edit->Advanced->Format Selection" command formats long lines? In particular, I want the command to indent long lines differently than new scope. By default, the...
3
by: Chua Wen Ching | last post by:
I have a problem. But on .NET 1.1 My Scenario: Actually I will have a string of hexadecimals read from a xml file. Then from the hexadecimals, i will add 1 value whenever i made any...
17
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
7
by: wenmang | last post by:
what is format for sprintf to convert long long integer (64 bits) to string?
7
by: wenmang | last post by:
Hi all, I have a confusion for representation of hex number: For an unsigned long long number, its max value is: unsigned long long maxNum = 18446744073709551615; if I do: printf("max uint64_t...
8
by: elrondrules | last post by:
Hi I am running a HTTP server which receives post from a process. In my do_POST method am receiving raw data. I know that this raw data has a valid XML content and I need to convert this into...
15
by: shiniskumar | last post by:
How to convert string to double? Ive got a double variable dTot; its value is 5.037717235E7 when i did FreemarkerTools.formatDecimal(dTot) i got it as string "50377172.35". now i want to...
4
by: thomasc1020 | last post by:
This is regarding VB.NET 2003. Variable 'Date' is a string and it contains date information in this format: "DEC/05/2007". Now I am trying to convert the format as "2007-12-05". Is it...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.