I am writing code in python that can not only read a xml but also send the results of that parsing as an email. Now I am having trouble just trying to read the file I have in xml. I made a simple python script that I thought would at least read the file which I can then try to email within python but I am getting a Syntax Error in line 4.
root.tag ‘log’
Anyways here is the code I written so far:
import xml.etree.cElementTree as etree
tree = etree.parse('C:/opidea.xml')
response = tree.getroot()
log = response.find('log').text
logentry = response.find('logentry').text
author = response.find('author').text
date = response.find('date').text
msg = [i.text for i in response.find('msg')]
Now the xml file has this type of formating
<log>
<logentry
revision="12345">
<author>glv</author>
<date>2012-08-09T13:16:24.488462Z</date>
<paths>
<path
action="M"
kind="file">/trunk/build.xml</path>
</paths>
<msg>BUG_NUMBER:N/A
FEATURE_AFFECTED:N/A
OVERVIEW:Example</msg>
</logentry>
</log>
I want to be able to send an email of this xml file. For now though I am just trying to get the python code to read the xml file.
response.find('log')won’t find anything, because:In your case
logis not a subelement, but rather the root element itself. You can get its text directly, though:response.text. But in your example thelogelement doesn’t have any text in it, anyway.EDIT: Sorry, that quote from the docs actually applies to
lxml.etreedocumentation, rather thanxml.etree.I’m not sure about the reason, but all other calls to
findalso returnNone(you can find it out by printingresponse.find('date')and so on). Withlxmlou can usexpathinstead:In any case, your use of
findis not correct formsg, becausefindalways returns a single element, not a list of them.