Files
smart-management-auto-test/base_framework/public_tools/xml_file_api.py
qiaoxinjiu 6994b185a3 addproject
2026-01-22 19:10:37 +08:00

39 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding:utf-8 -*-
# 功能对xml文件的操作函数
import os
import sys
import xml.etree.ElementTree as ET
class XmlFileApi:
def __init__(self, file_path):
self.file_path = file_path
self.root = ET.parse(file_path).getroot()
def get_contain_by_tag_names(self, tag_names):
"""
功能根据多级标签名查询xml文件中的内容
Args:
tag_names: 多级标签名,从根目录开始,中间用/分隔例如statistics/total/stat
Returns:
对应标签名的内容,格式:[{"text": xxx, "attrib": yyy}]
"""
# for child in self.root:
# print(child.tag, child.attrib)
elements = self.root.findall('.//' + tag_names)
if len(elements) == 0:
print("根据标签:{},未找到对应的内容,请检查标签名是否正确".format(tag_names))
return
return_data = []
for element in elements:
return_data.append({"text": element.text, "attrib": element.attrib})
return return_data
if __name__ == '__main__':
xf = XmlFileApi(file_path="C:/Users/17447/Downloads/output_gue_it.xml")
xf.get_contain_by_tag_names(tag_names='statistics/total/stat')
# C:/Users/17447/Downloads/output_gue_it.xml
# C:/Users/17447/Downloads/output_gue_it.xml