0

I am trying to use one of the standard python3 libs to parse some data, that is being returned as XML, from a website. I just want something really simple. I am not doing anything that complex.

It seems like xml.etree.ElementTree is exactly what I want. Except, the parse function takes a file name parameter, instead of a buffer which contains the xml. I don't see a class member that seems to take a buffer/string. If it were the other way around, you could easily still read in that file and parse it with "ET.Parse(fd.read(-1))" and you are done. But, alas, the function requires a filename. I can work around this by writing to a file in /tmp, then parsing it. But, that seems silly (to be polite) to write to disk, only to read it back in and parse it.

Basically, I want:

xml.etree.ElementTree.parse(xmlbuffer)

Where, xmlbuffer is a string, not a filename.

Any ideas?

Mark Hattarki
  • 123
  • 2
  • 8
  • 2
    You want `fromstring()`: https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.fromstring – mzjn Jul 13 '22 at 19:17
  • Yes, but there is a subtle difference: import xml.etree.ElementTree as ET tree = ET.parse('country_data.xml') root = tree.getroot() is equivalent to: root = ET.fromstring(country_data_as_string) Note that there is no "tree" class. This continued some of the confusion. I don't think I need the "tree" class, so this works. But, I do wonder why that step would be needed in either method (from file or buffer). – Mark Hattarki Jul 13 '22 at 19:26
  • The documentation clearly states that `fromstring()` returns an `Element` and that `parse()` returns an `ElementTree` instance. – mzjn Jul 13 '22 at 19:31

0 Answers0