elementtree

Discussion in 'Python' started by elsa, Aug 24, 2009.

  1. elsa

    elsa Guest

    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!
     
    elsa, Aug 24, 2009
    #1
    1. Advertising

  2. 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
     
    Stefan Behnel, Aug 24, 2009
    #2
    1. Advertising

  3. elsa

    Dave Angel Guest

    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
     
    Dave Angel, Aug 24, 2009
    #3
  4. 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
     
    Stefan Behnel, Aug 24, 2009
    #4
  5. elsa

    Carl Banks Guest

    On Aug 24, 12:13 am, 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?


    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
     
    Carl Banks, Aug 24, 2009
    #5
  6. On Aug 24, 7:29 pm, Dave Angel <> 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
     
    Nadia Johnson, Aug 25, 2009
    #6
  7. elsa

    Kee Nethery Guest

    elementtree examples

    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
     
    Kee Nethery, Oct 19, 2009
    #7
  8. elsa

    Paul Rubin Guest

    Re: elementtree examples

    Kee Nethery <> 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.
     
    Paul Rubin, Oct 19, 2009
    #8
  9. elsa

    Kee Nethery Guest

    Re: elementtree examples

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

    > Kee Nethery <> 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
     
    Kee Nethery, Oct 19, 2009
    #9
  10. elsa

    Robert Kern Guest

    Re: elementtree examples

    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
     
    Robert Kern, Oct 19, 2009
    #10
  11. elsa

    Kee Nethery Guest

    Re: elementtree examples

    On Oct 19, 2009, at 1:02 PM, Robert Kern wrote:

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


    Thank you.
    Kee
     
    Kee Nethery, Oct 20, 2009
    #11
  12. Re: elementtree examples

    En Mon, 19 Oct 2009 17:02:44 -0300, Robert Kern <>
    escribió:
    > 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.


    Well, not so detailed, but the selftest.py module in the source package
    for elementtree (available from www.effbot.org) does provide quite a few
    examples using doctest.

    --
    Gabriel Genellina
     
    Gabriel Genellina, Oct 20, 2009
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Arnold

    elementtree behavior

    Tim Arnold, Sep 27, 2004, in forum: Python
    Replies:
    2
    Views:
    253
    Fredrik Lundh
    Sep 27, 2004
  2. Jesper Olsen

    elementtree -

    Jesper Olsen, Oct 3, 2004, in forum: Python
    Replies:
    2
    Views:
    538
    Jesper Olsen
    Oct 4, 2004
  3. Stewart Midwinter

    xml file structure for use with ElementTree?

    Stewart Midwinter, Oct 9, 2004, in forum: Python
    Replies:
    7
    Views:
    366
    Stewart Midwinter
    Oct 11, 2004
  4. ElementTree and XPATH

    , Dec 9, 2004, in forum: Python
    Replies:
    3
    Views:
    355
    Martijn Faassen
    Dec 11, 2004
  5. Kee Nethery
    Replies:
    12
    Views:
    2,094
    Stefan Behnel
    Jun 27, 2009
Loading...

Share This Page