[Tutor] Issues Parsing XML
Stefan Behnel
stefan_ml at behnel.de
Thu Mar 12 20:47:24 CET 2009
marc at marcd.org wrote:
> I am new to Python and as a first project decided to try to parse an XML
> report using Python. I have the following, which works to extract one
> element. I am stuck, however, at one element. I want to extract several
> differenct elements per line, creating a comma separated variable (CSV)
> line that can be imported to a spreadsheet. Not all elements are in each
> line or part of the XML document - so if an element is not in a line, I
> would leave a blank (2 commas). I can probably figure that out - it's the
> extracting multiple elements and putting them in one line that has me
> stumped. Help would be greatly appreciated. Thank you. What I have so
> far (and I would like to stick to the DOM model):
There is another "DOM Model" in the stdlib. It's called ElementTree and is
generally a lot easier to use. For example, to find the text content of an
element called "element_that_has_text_content" in a subtree below
"some_element", you can do
print some_element.findtext(".//element_that_has_text_content")
Stefan
More information about the Tutor
mailing list