ElementTree should parse string and file in teh same way



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


.



Relevant Pages

  • Re: ElementTree should parse string and file in the same way
    ... have you considered asking *why* ElementTree does not treat files and strings the same way? ... It may be that Fredrik doesn't agree with you that you should be able to parse a string and a file the same way, in which case there's nothing you can do but work around it. ...
    (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: ElementTree.XML(string XML) and ElementTree.fromstring(string XML) not working
    ... fromstring when used with a string do not do the same thing as parse ... Why do you need an ElementTree rather than an Element? ... Why not enhance parse and deprecate XML and fromstring with ... If you want to treat a string as a file, ...
    (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)