python xml解析

一、问题来源
需要列取android7.0原生态定义的权限
二、问题分析
1)系统权限定义的位置

frameworks\base\core\res\AndroidManifest.xml

2)系统权限定义的位置翻译对应

frameworks\base\core\res\res\values-zh-rCN\strings.xml

三、直接上代码
getPermission.py

#coding=utf-8
import build_csv_file
import collections
import xml.dom.minidom
from xml.dom.minidom import Document

class Excel_to_android_stringxml_obj:
    def __init__(self):


        self.xmlStringdict=collections.OrderedDict()




    def getXmlNodeValue(self, childNodes):

        value = ''
        for childNode in childNodes:

            if childNode.nodeName == '#text':
                value = value + childNode.wholeText
                # return childNode.wholeText
            value_key = ''
            if childNode.nodeName != '#text' and childNode.nodeName != '#comment':
                attributeDict = childNode._attrs
                if attributeDict != None:
                    for k, v in attributeDict.items():
                        value_key = value_key + ' %s="%s" ' % (k, childNode.getAttribute(k))
                text = self.getXmlNodeValue(childNode.childNodes)
                # print (text)
                value = value + "<%s%s>%s</%s>" % (childNode.nodeName, value_key, text, childNode.nodeName)
                # print(value)
        return value

    def getXmlNodeValue(self, childNodes):

        value = ''
        for childNode in childNodes:

            if childNode.nodeName == '#text':
                value = value + childNode.wholeText
                # return childNode.wholeText
            value_key = ''
            if childNode.nodeName != '#text' and childNode.nodeName != '#comment':
                attributeDict = childNode._attrs
                if attributeDict != None:
                    for k, v in attributeDict.items():
                        value_key = value_key + ' %s="%s" ' % (k, childNode.getAttribute(k))
                text = self.getXmlNodeValue(childNode.childNodes)
                # print (text)
                value = value + "<%s%s>%s</%s>" % (childNode.nodeName, value_key, text, childNode.nodeName)
                # print(value)
        return value




        # 解析xml

    def parse_strings_xml_file(self, xml_fname):
        try:
            DOMTree = xml.dom.minidom.parse(xml_fname)
            collection = DOMTree.documentElement

            stringList = collection.getElementsByTagName("string")

            for string in stringList:
                if string.hasAttribute("name"):
                    key = string.getAttribute("name")
                    # print("name: %s" % key)

                    value = self.getXmlNodeValue(string.childNodes)

                    # print("value: %s" % value)

                    self.xmlStringdict[key] = value
            return True
        except Exception as e:
            print(e)
            return False



    # 解析xml

    def parse_AndroidManifest_xml_file(self, xml_fname):
        try:
            DOMTree = xml.dom.minidom.parse(xml_fname)
            collection = DOMTree.documentElement

            stringList = collection.getElementsByTagName("permission")
            for string in stringList:
                if string.hasAttribute("android:name"):
                    android_name= string.getAttribute("android:name")
                    print("android_name: %s" % android_name)
                else:
                    android_name=''
                if string.hasAttribute("android:permissionGroup"):
                    android_permissionGroup= string.getAttribute("android:permissionGroup")
                    print("android_permissionGroup: %s" % android_permissionGroup)
                else:
                    android_permissionGroup=''
                if string.hasAttribute("android:protectionLevel"):
                    android_protectionLevel= string.getAttribute("android:protectionLevel")
                    print("android_protectionLevel: %s" % android_protectionLevel)
                else:
                    android_protectionLevel=''
                if string.hasAttribute("android:label"):
                    android_label = string.getAttribute("android:label")[8:]
                    android_label_str=self.xmlStringdict[android_label]
                    print("android_label: %s" % android_label_str)
                else:
                    android_label_str = ''
                if string.hasAttribute("android:description"):
                    android_description = string.getAttribute("android:description")[8:]
                    android_description_str = self.xmlStringdict[android_description]
                    print("android_description_str: %s" % android_description_str)
                else:
                    android_description_str = ''

                dataList=[android_name,android_permissionGroup,android_protectionLevel,android_label_str,android_description_str]
                build_csv_file.build_csv_file("2.csv", dataList)
            return True
        except Exception as e:
            print(e)
            return False




obj = Excel_to_android_stringxml_obj()
build_csv_file.build_csv_file("2.csv", ['权限','权限组','    权限级别','标签','描述'])
obj.parse_strings_xml_file(r'Y:\6737\frameworks\base\core\res\res\values-zh-rCN\strings.xml')
obj.parse_AndroidManifest_xml_file(r"Y:\6737\frameworks\base\core\res\AndroidManifest.xml")

build_csv_file.py

#coding=utf-8
import csv
def build_csv_file(fileName,dataList):
    with open(fileName,'a',newline='',encoding='utf_8_sig') as csvfile:
        ww=csv.writer(csvfile, dialect='excel')
        ww.writerow(dataList)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值