python遍历XMLinminidom

python遍历XMLinminidom

2023年6月25日发(作者:)

python遍历XMLinminidom# -*- coding: utf-8 -*-#!/usr/local/bin/pythonimport mimport pdbimport os#Read Node's TEXT_NODE value

def getText(nodelist):

rc = []

for node in nodelist:

if pe == _NODE: () #If it's need trim ,use follow sentance

#(().rstrip()) return ''.join(rc)#Input parameter : fileName

#Read XML file struct like this

''' /alaska1131 '''def getInfoFromFile(fileName): try:

dom = (fileName)

urlDic = {} urlCategory = ''

urlValue = ''

for item in mentsByTagName("String"): if ribute("id"): #urlCategory ="[" + fileName[(""):len(fileName)-1] + "] " + utes["id"].value

urlCategory = utes["id"].value

urlValue = getText(odes) if len(urlValue) is not None or len(urlValue) > 0 : urlDic[urlCategory] = urlValue else :

print "ERROR :fileName[%s] t attributeValue[%s] t textNodeValue[%s] invalid"%(fileName,urlCategory,urlValue)

return urlDic

except:

print "loading file [%s]" % (fileName)

return None

if __name__ == "__main__":

urlDic = {} urlDicTMP = {} parentPath = "D:codeloadXMLFile" fileList = r(parentPath) for fileName in fileList :

urlDicTMP = getInfoFromFile(parentPath + "" + fileName) if urlDicTMP is None :

print "fileName :[%s] is None "%(fileName) continue

for key in urlDicTMP: #whether this value has been set in urlDic

if _key(key) :

print "WARNING : fileName[%s] t key[%s] has been set."%(fileName ,key) else : urlDic[key] = urlDicTMP[key]

print "-"* 70 for key in urlDic: print "%s === %s"%(key,urlDic[key])

#("%s === %s",key,urlDic[key])

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1687678443a30852.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信