重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Python——使用ElementTree解析AndroidManifest.xml文件

  1. XML内容如下:

    红安网站建设公司成都创新互联,红安网站设计制作,有大型网站制作公司丰富经验。已为红安1000+提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的红安做网站的公司定做!



    
    
        
            
                
                
            
        
        
        
    

2.解析过程:主要获取开放activity的信息,并保存到xml文件中

from ActivityInfo import ActivityInfo

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET
import sys

# Manifest namespace
namespace = '{http://schemas.android.com/apk/res/android}'
# Exported activities
exported_activities = []


# XML analyze
def __xml_analyze__():
    tree = ET.parse(r"./app/AndroidManifest.xml")
    root = tree.getroot()
    # print tree.namespace
    # 1.get package name
    packagename = root.attrib["package"]
    # 2.get exported activity
    application = root.find("application")
    __get_exported_activities__(application, packagename)
    for activity in exported_activities:
        print activity.__get_name__(), activity.__get_package_name__(), activity.__get_permission__()
    # 3.save result to xml file
    __save_result_to_xml__()


# get exported activities
def __get_exported_activities__(application, packagename):
    for activity in application:
        # 2.1 MainActivity name
        if __is_main_activity__(activity):
            name = activity.attrib[namespace+'name']
            main_activtiy = ActivityInfo(name)
            main_activtiy.__set_package_name__(packagename)
            if activity.find(namespace+'permission') is not None:
                permission = activity.attrib[name+'permission']
                main_activtiy.__set_permission__(permission)
            exported_activities.append(main_activtiy)
        else:
            # 2.1 exported activities except mainactivtiy
            exported = activity.attrib[namespace+'exported']
            if exported:
                activity_name = activity.attrib[namespace+'name']
                exported_activity = ActivityInfo(activity_name)
                exported_activity.__set_package_name__(packagename)
                permission = activity.attrib.get(namespace+'permission', None)
                if permission is not None:
                    # permission = activity.attrib[name+'permission']
                    # print permission
                    exported_activity.__set_permission__(permission)
                exported_activities.append(exported_activity)


# save result to xml file
def __save_result_to_xml__():
    tree = ET.ElementTree()
    root = ET.Element('data')
    tree._setroot(root)
    for activity in exported_activities:
        node = ET.Element('activity')
        node.set('name', activity.__get_name__())
        node.set('packagename', activity.__get_package_name__())
        root.append(node)
        if activity.__get_permission__() is not None:
            node.set('permission', activity.__get_permission__())
    tree.write("output.xml")


# the activity is or not MainActivity
def __is_main_activity__(activity):
    intent_filter = activity.find("intent-filter")
    if intent_filter is not None:
        action = intent_filter.find('action')
        category = intent_filter.find('category')
        if action is not None and category is not None \
                and action.attrib[namespace + "name"] == 'android.intent.action.MAIN'\
                and category.attrib[namespace + "name"] == 'android.intent.category.LAUNCHER':
            print 'this is main activity'
            return True
    return False


# main fun
if __name__ == "__main__":
    __xml_analyze__()

3.ActivityInfo 

class ActivityInfo:

    def __init__(self, name):
        self.name = name
        self.package_name = ''
        self.permission = ''

    def __set_package_name__(self, package_name):
        self.package_name = package_name

    def __get_package_name__(self):
        return self.package_name

    def __set_name__(self, name):
        self.name = name

    def __get_name__(self):
        return self.name

    def __set_permission__(self, permission):
        self.permission = permission

    def __get_permission__(self):
        return self.permission


网页标题:Python——使用ElementTree解析AndroidManifest.xml文件
URL地址:http://cqcxhl.com/article/gdoihh.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP