Re: ElementTree should parse string and file in the same way



Peter Pei wrote:
One bad design about elementtree is that it has different ways parsing a
string and a file, even worse they return different objects:
1) When you parse a file, you can simply call parse, which returns a
elementtree, on which you can then apply xpath;

ElementTree doesn't support XPath. In case you mean the simpler ElementPath
language that is supported by the find*() methods, I do not see a reason why
you can't use it on elements.


2) To parse a string (xml section), you can call XML or fromstring, but
both return element instead of elementtree. This alone is bad. To make
it worse, you have to create an elementtree from this element before you
can utilize xpath.

a) how hard is it to write a wrapper function around fromstring() that wraps
the result Element in an ElementTree object and returns it?

b) the same as above applies: I can't see the problem you are talking about.

Stefan
.



Relevant Pages

  • Re: ElementTree and XPATH
    ... > the location steps of xpath. ... an elementtree API compatible interface will indeed extend that API and ...
    (comp.lang.python)
  • xml file structure for use with ElementTree?
    ... I want to parse a file with ElementTree. ... and add an element to the tree. ... input file structure. ...
    (comp.lang.python)
  • Re: How *extract* data from XHTML Transitional web pages? got xml.dom.minidom troubles..
    ... WHATWG "HTML5" spec which is in turn based on the behavior of major web browsers so it should parse more or less* any invalid markup you throw at it. ... By default, you have the option of producing a minidom tree, an ElementTree, or a "simpletree" - a lightweight DOM-like html5lib-specific tree. ... There might be a problem if e.g. the document uses a character encoding that python does not support, ...
    (comp.lang.python)
  • Re: Parsing XML with ElementTree (unicode problem?)
    ... amazon response) but fails to parse the locally saved file. ... I've just used wget to fetch that URL and `ElementTree` parses that local ... `ElementTree` and accept having a broken XML file on your disk. ...
    (comp.lang.python)
  • Re: ElementTree should parse string and file in the same way
    ... XPath is not fully supported. ... When you parse a file, you can simply call parse, which returns a ... ElementTree doesn't support XPath. ...
    (comp.lang.python)