0

以下のpythonコードを使用してxmlファイル(以下のデータ)を読み込もうとしていますが、xmlファイルemail.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>]

パイソンコード:

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

1 に答える 1

2

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
于 2012-11-18T22:47:32.237 に答える