I am trying to read the xml file email.xml(Data below) using the python code provided below,am not able to print the actual data present in the xml file but getting the below output. Where am I going wrong?
CURRENT OUTPUT:
xmlfile
<open file 'email.xml', mode 'r' at 0x0226AF98>
[<DOM Element: to at 0x231d620>]
[<DOM Element: cc at 0x231d6c0>]
[<DOM Element: bcc at 0x231d760>]
PYTHON CODE:
import xml.dom.minidom as minidom
def getemaildata():
# Open the XML file
xmlfile = open('email.xml','r')
print "xmlfile"
print xmlfile
dom = minidom.parse(xmlfile)
email=dom.getElementsByTagName('email')
for node in email:
toemail=dom.getElementsByTagName('to')
print toemail
ccemail=dom.getElementsByTagName('cc')
print ccemail
bccemail=dom.getElementsByTagName('bcc')
print bccemail
return (toemail,ccemail,bccemail)
def main ():
(To,CC,BCC)=getemaildata()
if __name__ == '__main__':
main()
email.xml file:
<email>
<to>data@company.com;data.stability@company.com;
data.sns@company.com;data.pes@company.com;</to>
<cc> data.team </cc>
<bcc>data@company.com</bcc>
</email>
You are getting back lists of “Element” objects from the XML parser. You need to iterate further to get to the actual “Text” nodes.
Eg:
To clean the addresses from the text node: