ElementTree and proper identation?

J

John Salerno

I've been doing a little studying of ElementTree and it doesn't seem
very satisfactory for writing XML files that are properly
formatted/indented. I saw on the website that there is an
indent/prettyprint function, but this isn't listed in the Python docs
and I didn't see it after doing a dir(), so I guess it isn't a part of
the Python version.

Did I miss something somewhere else, or can you just not use ElementTree
to write formatted XML files as they'd look if done by hand?

Thanks.
 
J

John Salerno

John said:
I've been doing a little studying of ElementTree and it doesn't seem
very satisfactory for writing XML files that are properly
formatted/indented. I saw on the website that there is an
indent/prettyprint function, but this isn't listed in the Python docs
and I didn't see it after doing a dir(), so I guess it isn't a part of
the Python version.

Did I miss something somewhere else, or can you just not use ElementTree
to write formatted XML files as they'd look if done by hand?

Thanks.

Well, further research reveals that this function isn't available yet in
any version. But I guess my question remains, is there still a way to do
this, given the way Element instances are constructed? I would assume
you can't just use ElementTree to write them to a file, but perhaps
there is a different way to write to a file that still uses the Element
objects?
 
?

=?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=

John Salerno a écrit :
I've been doing a little studying of ElementTree and it doesn't seem
very satisfactory for writing XML files that are properly
formatted/indented. I saw on the website that there is an
indent/prettyprint function, but this isn't listed in the Python docs
and I didn't see it after doing a dir(), so I guess it isn't a part of
the Python version.

It *may* be included in a *future* release of ElementTree. For now, why
don't
you simply drop the 12 lines of codes of the prettyprint function into
your
project ? Source available at http://effbot.org/zone/element-lib.htm

Cheers,

SB
 
F

Fredrik Lundh

John said:
Well, further research reveals that this function isn't available yet in
any version.

what keeps you from using the source code on the page you're talking about ?

</F>
 
W

Wade Leftwich

You can roll your own by starting at the root and recursing through
child elements, increasing the indentation as you go:

But I do it the lazy way:

$ xmllint --format ugly.xml >pretty.xml

-- Wade Leftwich
Ithaca, NY
 
J

John Salerno

Fredrik said:
what keeps you from using the source code on the page you're talking
about ?

</F>

Yeah, actually I just gave this a try but I couldn't figure out where
the code needed to go or how to call the function. I tried creating an
Element with a few SubElements, then I did:

print >> filename, indent(root)

But that only wrote the value "None" into the file.
 
J

John Salerno

John said:
Yeah, actually I just gave this a try but I couldn't figure out where
the code needed to go or how to call the function. I tried creating an
Element with a few SubElements, then I did:

print >> filename, indent(root)

But that only wrote the value "None" into the file.

Ok, I see that indent() returns None. In that case, how do I get the
formatted text that it produces?
 
J

John Salerno

John said:
Ok, I see that indent() returns None. In that case, how do I get the
formatted text that it produces?

Ok, through the magic of sight I noticed that it is an inplace change,
so I tried indent(root) and then wrote root to a file with ElementTree
as normal. Very nice!
 
R

Robert Kern

John said:
John Salerno wrote:

Ok, I see that indent() returns None. In that case, how do I get the
formatted text that it produces?

The indent() function modifies the elements themselves to add the indentation.
So dump the tree to a string.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top