python xml解析
2017-09-29 09:25
525 查看
一、问题来源
需要列取android7.0原生态定义的权限
二、问题分析
1)系统权限定义的位置
2)系统权限定义的位置翻译对应
三、直接上代码
getPermission.py
build_csv_file.py
需要列取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)
相关文章推荐
- Python解析xml中dom元素的方法
- python对XML的解析
- python 执行c 程序解析 xml 写入文本文件统计程序
- python之自动生成解析xml的C++类
- python 解析xml
- python网络编程学习笔记(8):XML生成与解析(DOM、ElementTree)
- python对XML的解析(简略分析)
- python 模块- 对XML的解析 ElementTree(元素树)
- python解析XML文件
- python 解析XML
- python-minidom模块【解析xml】
- 简单介绍使用Python解析并修改XML文档的方法
- 用 ElementTree 在 Python 中解析 XML
- Python解析XML的minidom
- python解析xml
- python3.4.3 调用http接口 解析response xml后插入数据库
- Python解析生成XML-ElementTree VS minidom
- python cookbook第三版学习笔记七:python解析csv,json,xml文件
- python网络编程学习笔记(八):XML生成与解析(DOM、ElementTree)
- python 解析XML(拼合互联网资料学习整理)