无法获取xml文件输出

2024-04-26 00:08:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用下面提供的python代码读取xml文件email.xml(下面的数据),无法打印xml文件中的实际数据,但得到下面的输出。我哪里出错了?你知道吗

电流输出:

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代码

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文件

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

Tags: 文件tocomdataemailxmlcompanyat
1条回答
网友
1楼 · 发布于 2024-04-26 00:08:07

您将从XML解析器中获取“Element”对象的列表。您需要进一步迭代以获得实际的“文本”节点。你知道吗

例如:

# this returns a list of all Elements that have the tag "to"
toemail=dom.getElementsByTagName('to')

# Here we take the first node returned with tag 'to', then it's first child node
textnode = toemail[0].childNodes[0]

# print the data in the textnode
print textnode.data

要清除文本节点中的地址,请执行以下操作:

for address in textnode.data.split(';'):
    if address == '':
        # Catch empty entries as a result of trailing ;
        continue
    email = i.strip().strip('\n')
    print email

相关问题 更多 >