elementtree

E

elsa

I have a question about elementtree:

I know how to turn HTML into an ElementTree object, but I don't know
how to then view the structure of this object. Is there a method or
module that you can give an ElementTree object to, and it returns some
kind of graphical or printed representation of the tree? Otherwise, if
you can't see you're tree's structure, how do you know what is a
sensible way of iterating over the tree to access the info you need?

Thanks!
 
S

Stefan Behnel

Hi,
I know how to turn HTML into an ElementTree object

I don't. ;)

ElementTree doesn't have an HTML parser, so what do you use for parsing?

but I don't know
how to then view the structure of this object. Is there a method or
module that you can give an ElementTree object to, and it returns some
kind of graphical or printed representation of the tree? Otherwise, if
you can't see you're tree's structure, how do you know what is a
sensible way of iterating over the tree to access the info you need?

ElementTree has a tostring() method that returns a string. To get a pretty
printed representation, you can use the indent() function from this recipe:

http://effbot.org/zone/element-lib.htm#prettyprint

Stefan
 
D

Dave Angel

Stefan said:
Hi,


I don't. ;)

ElementTree doesn't have an HTML parser, so what do you use for parsing?




ElementTree has a tostring() method that returns a string. To get a pretty
printed representation, you can use the indent() function from this recipe:

http://effbot.org/zone/element-lib.htm#prettyprint

Stefan
Perhaps the OP was referring to XHTML, which should be eligible for
ElementTree. But could you tell me whether ElementTree is at all
tolerant of malformed XML? Most HTML and XHTML I encounter in the wild
is so buggy it's amazing it all works at all.


DaveA
 
S

Stefan Behnel

Dave said:
Perhaps the OP was referring to XHTML, which should be eligible for
ElementTree. But could you tell me whether ElementTree is at all
tolerant of malformed XML? Most HTML and XHTML I encounter in the wild
is so buggy it's amazing it all works at all.

Well, if the XHTML is "buggy", it's not XHTML at all. XHTML is XML, which
is defined as being well-formed. Any XHTML parser is required to reject
malformed input, and the expat parser that ElementTree uses is (luckily) no
exception.

Regarding malformed HTML: that's not directly supported by ElementTree,
hence my question. You can use ElementSoup to interface with BeautifulSoup,
or elementtidy to interface with tidy, or html5lib with ElementTree as
backend, or you can use lxml instead, which handles malformed HTML (and is
all fast and shiny and ... ;).

Stefan
 
C

Carl Banks

Hi,


I don't. ;)

ElementTree doesn't have an HTML parser, so what do you use for parsing?

The OP could be feeding the HTML through tidy, or it could be XHTML.

ElementTree has a tostring() method that returns a string. To get a pretty
printed representation, you can use the indent() function from this recipe:

http://effbot.org/zone/element-lib.htm#prettyprint

Another possibility is to write out the ElementTree object as XML with
an .xml extension, and view it in a modern web browser (Firefox, IE,
others maybe) that can show XML structure.


Carl Banks
 
N

Nadia Johnson

Perhaps the OP was referring to XHTML, which should be eligible for
ElementTree.  But could you tell me whether ElementTree is at all
tolerant of malformed XML?   Most HTML and XHTML I encounter in the wild
is so  buggy it's amazing it all works at all.

DaveA

I used elementtidy, also available from effbot
 
K

Kee Nethery

The official elementtree docs:
http://docs.python.org/library/xml.etree.elementtree.html

do not appear to contain examples for each object, interface or
function. Where would I find examples that use each elementtree
function, interface and object?

I assume there is some kind of regression testing that is performed on
the Python code base and that the regression testing probably has many
many examples.

Is there a link in the official documentation to those regression test
examples? If not, how do I get a link added to the official docs so
that I can view all the examples used to test code changes to the
various modules?

Kee Nethery
 
P

Paul Rubin

Kee Nethery said:
do not appear to contain examples for each object, interface or
function. Where would I find examples that use each elementtree
function, interface and object?

effbot.org has a few.
 
K

Kee Nethery

effbot.org has a few.

yes I agree it has a few. It's not anywhere close to complete. I am
looking for examples for each object, interface or function.

If you just take the very first function in the official docs as an
example:
xml.etree.ElementTree.Comment([text])

The effbot.org site appears to contain no working examples of that
function. At a minimum it would be kind of nice for two simple
examples, one with str and one with unicode "text" showing the XML
before and after this function acts upon it.

Am looking for that level of documentation for each function,
interface and object listed in the official docs for elementtree.

Does it exist?

Thanks, Kee Nethery
 
R

Robert Kern

Am looking for that level of documentation for each function, interface
and object listed in the official docs for elementtree.

Does it exist?

No.

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top