from xml.etree import ElementTree as ET
import os
NewFilePath = "D:\parseXml\\00001_00003_00000.xml"
OldFilePath = "D:\parsexml1\\00001_00001_00000.xml"
def getXmlFiledir(FindPath):
suffix='.xml'
FileList = []
FileNames = os.listdir(FindPath)
for filename in FileNames:
name, suf = os.path.splitext(filename)
if suf == suffix:
FileList.append(os.path.join(FindPath, filename))
return FileList
tags = []
attr = []
def root(childs):
for child in childs:
if (child.getchildren() is None):
break
else:
attr.append(child.attrib)
tags.append(child.tag)
root(child)
def newattrList(NewRoot):
root(NewRoot)
newattr=attr.copy()
attr.clear()
return newattr
def oldattrList(OldRoot):
root(OldRoot)
oldattr=attr.copy()
return oldattr
def compareToxml(NewFilePath,OldFilePath):
tag ={ 1 : "新的标签数量多" , 2 : "新的标签数量少", 3 : "属性值相同" , 4 : "属性值不相同" }
NewTree = ET.parse(NewFilePath)
Oldtree = ET.parse(OldFilePath)
NewRoot = NewTree.getroot()
OldRoot = Oldtree.getroot()
oldattr =oldattrList(OldRoot)
oldtags = tags.__len__()
tags.clear()
oldattr.insert(0,OldRoot.attrib)
attr.clear()
newattr = newattrList(NewRoot)
newtags = tags.__len__()
tags.clear()
newattr.insert(0,NewRoot.attrib)
if (oldtags < newtags):
return tag.get(1)
elif (oldtags > newtags):
return tag.get(2)
if(oldattr == newattr):
return tag.get(3)
return tag.get(4)
if __name__ == "__main__":
print(compareToxml(NewFilePath,OldFilePath))