Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > elementtree

Reply
Thread Tools

elementtree

 
 
elsa
Guest
Posts: n/a
 
      08-24-2009
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!
 
Reply With Quote
 
 
 
 
Stefan Behnel
Guest
Posts: n/a
 
      08-24-2009
Hi,

elsa wrote:
> 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
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      08-24-2009
Stefan Behnel wrote:
> Hi,
>
> elsa wrote:
>
>> 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
>
>

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
 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      08-24-2009
Dave Angel wrote:
> Stefan Behnel wrote:
>> elsa wrote:
>>> 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?
>>

> 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
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      08-24-2009
On Aug 24, 12:13*am, Stefan Behnel <(E-Mail Removed)> wrote:
> Hi,
>
> elsa wrote:
> > 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?


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


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


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
 
Reply With Quote
 
Nadia Johnson
Guest
Posts: n/a
 
      08-25-2009
On Aug 24, 7:29*pm, Dave Angel <(E-Mail Removed)> wrote:
> Stefan Behnel wrote:
> > Hi,

>
> > elsa wrote:

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

>
> 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
 
Reply With Quote
 
Kee Nethery
Guest
Posts: n/a
 
      10-19-2009
The official elementtree docs:
http://docs.python.org/library/xml.e...ementtree.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

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      10-19-2009
Kee Nethery <(E-Mail Removed)> writes:
> 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.
 
Reply With Quote
 
Kee Nethery
Guest
Posts: n/a
 
      10-19-2009

On Oct 19, 2009, at 12:06 PM, Paul Rubin wrote:

> Kee Nethery <(E-Mail Removed)> writes:
>> 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.


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

 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      10-19-2009
On 2009-10-19 14:50 PM, Kee Nethery wrote:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ElementTree.XML(string XML) and ElementTree.fromstring(string XML)not working Kee Nethery Python 12 06-27-2009 06:06 AM
ElementTree and XPATH dayzman@hotmail.com Python 3 12-11-2004 02:54 PM
xml file structure for use with ElementTree? Stewart Midwinter Python 7 10-11-2004 05:41 AM
elementtree - Jesper Olsen Python 2 10-04-2004 07:12 AM
elementtree behavior Tim Arnold Python 2 09-27-2004 08:41 PM



Advertisments