Python是非常强大的工具,用它解析XML非常快速,而且可以迅速的处理自己想要的数据,以下是示例代码:
import xml.etree.ElementTree as etree
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
def is_info_in_xml( xml_filename):
'''精确检查XML文档中被查找信息是否存在'''
info_exists = False
xml_document = etree.parse(xml_filename)
root = xml_document.getroot() # 获取根元素
# print('xml file root:', root) # 获取根名称
# print('root:%s' % root ) # 获取根元素的属性集
# print('%s %s="%s"' % (root.tag, root.tag, root.attrib) ) # 获取根元素的属性集
# print('root len:', len(root)) # 获取根元素的长度(也就是根元素下的子元素的个数)
# 检查root节点下的所有子节点
for softwarelist in root: # 处理下一级子节点SoftwareList
# print('\t%s' % softwarelist.tag, end = ' ')
for attrname in softwarelist.attrib:
# print('%s="%s"' % (attrname, softwarelist.attrib[attrname]), end = ' ') # 获取元素的属性集
pass
# print()
for group in softwarelist: # 处理下一级子节点Group
# print('\t%s' % group.tag, end = ' ')
for attrname in group.attrib:
# print('%s="%s"' % (attrname, group.attrib[attrname]), end = ' ') # 获取元素的属性集
pass
# print()
for software in group: # 处理下一级子节点Software
# print('\t\t%s' % software.tag)
for attrname in software.attrib:
# print('\t\t\t%s="%s"' % (attrname, software.attrib[attrname])) # 获取元素的属性集
if '查找字符串' in software.attrib[attrname]:
info_exists = True
# print()
return info_exists
推荐阅读: