醉里乾坤大,壶中日月长

python minidom handle xml

上一篇 / 下一篇  2009-07-29 10:08:47

在网络上没找到啥能够详细说明:load xml-->read element data-->replace element data-->save the xml整个流程的minidom处理xml流程的代码,因为要写一个脚本,其中要求是数据和程序分离,也就是用xml来保存数据作为配置文件导入,而python只用于数据读取和处理,写的一个函数,基本整个流程都覆盖了,记录:

from xml.dom import minidom

def xml_parse(filepath): #filepath='mailInform.xml'
    '''
    这个函数主要用来处理xml文件,首先读取文件中的mail服务器地址和发件人
    (sender)邮件名,然后寻找pointer值为True的recever作为本次发送邮件的
    用户
    @filepath:待处理的xml文件地址
    '''
    msg_list=[]

    xmldoc=minidom.parse(filepath) #load the xml file from localhost
    root=xmldoc.documentElement            #get the root element

    #get the SMTP server msg
    mail_node=root.getElementsByTagName('mail')[0]
    mail_host=mail_node.childNodes[1].firstChild.data
    msg_list.append(mail_host)

    #get the sender msg
    sender_node=root.getElementsByTagName('sender')[0]
    sender=sender_node.childNodes[3].firstChild.data
    msg_list.append(sender)

    recever_nodes=root.getElementsByTagName('recevers')
    i=0
    for node in recever_nodes:
        if node.childNodes[9].firstChild.data == 'True':
            #print node.childNodes[3].firstChild.data
            recever_name=node.childNodes[1].firstChild.data.encode('gb2312') #get the recever name
            recever_mail=node.childNodes[3].firstChild.data                  #get the recever mail address
            checker_name=node.childNodes[5].firstChild.data.encode('gb2312') #get the checker name
            check_mail=node.childNodes[7].firstChild.data                    #get the checker mail address
            for msg  in [recever_name,recever_mail,checker_name,check_mail]:
                msg_list.append(msg)
            #set the pointer from 'True' to be 'false'
            node.childNodes[9].firstChild.replaceData(0,4,'false')
            break
        i=i+1
        continue

    #handle the next sender msg
    if i < len(root.getElementsByTagName('recevers'))-1:  #when the sender is not the last node in the xml
        recever_next=root.getElementsByTagName('recevers')[i+1]
        recever_next.childNodes[9].firstChild.replaceData(0,5,'True')
        #recever_next.childNodes[9].firstChild.appendData('True')
        #print recever_next.childNodes[9].firstChild.data
    else:                                                 #when the sender is the last node in the xml
        recever_next=root.getElementsByTagName('recevers')[0]
        recever_next.childNodes[9].firstChild.replaceData(0,5,'True')

    #save the xml after the handle   
    f=file(filepath, 'w')
    import codecs
    writer = codecs.lookup('utf-8')[3](f)
    xmldoc.writexml(writer, encoding='utf-8')
    writer.close()

    return msg_list

总结:之前写的时候是使用list来保存待发送邮件的用户列表,然后和预期多对比,如果遇到人的增加或者减少,就要修改代码,比较麻烦,这次使用xml后就不用修改脚本了,直接在xml中添加或者删除一个节点就OK了。
另外就是遇到不知道的用法从网络上找不到还是去看Python的源代码吧,写的都比较清楚,基本上从英文命名就可以看出来函数的用法了。

TAG:

 

评分:0

我来说两句

日历

« 2024-03-25  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 72672
  • 日志数: 106
  • 建立时间: 2009-06-05
  • 更新时间: 2011-09-09

RSS订阅

Open Toolbar