# -*- 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