python--parsing androidmanifest.xml files using ElementTree

Source: Internet
Author: User

    1. The XML content is as follows:

<?xml version= "1.0"  encoding= "Utf-8"  standalone= "no"? ><manifest xmlns:android= " Http://schemas.android.com/apk/res/android " package=" com.dongbao.wauoen.interappactivitytesting "  Platformbuildversioncode= " platformbuildversionname=" "6.0-2704002" >    < Uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>    < Application android:allowbackup= "true"  android:icon= "@mipmap/ic_launcher"  android:label= "@ String/app_name " android:supportsrtl=" true " android:theme=" @style/apptheme ">         <activity android:exported= "true"  android:name= " Com.dongbao.wauoen.interappactivitytesting.MainActivity ">             <intent-filter>                 <action andRoid:name= "Android.intent.action.MAIN"/>                 <category android:name= "Android.intent.category.LAUNCHER"/>             </intent-filter>         </activity>        <activity  android:enabled= "@bool/use_activity"  android:exported= "true"  android:icon= "@drawable/ic_ Chooser " android:label=" @string/choose_file " android:name=" Com.ipaulpro.afilechooser.FileChooserActivity "/>        < Activity android:exported= "true"  android:name= " Com.dongbao.wauoen.interappactivitytesting.ExampleActivity " android:permission=" android.permission.READ_ External_storage "/>    </application></manifest>

2. Parsing process: The main access to open activity information, and saved to the XML file

from activityinfo import activityinfotry:    import  xml.etree.celementtree as etexcept importerror:    import  xml.etree.elementtree as etimport sys# manifest namespacenamespace =  ' {http ://schemas.android.com/apk/res/android} ' # exported activitiesexported_activities = []# &NBSP;XML&NBSP;ANALYZEDEF&NBSP;__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) &NBSP;&Nbsp;  for activity in exported_activities:        &NBSP;PRINT&NBSP;ACTIVITY.__GET_NAME__ (),  activity.__get_package_name__ (),  activity.__get_permission __ ()     # 3.save result to xml file    __save _result_to_xml__ () # get exported activitiesdef __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 ']             &NBSP;&NBSP;&NBSP;&NBSP;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 ']&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;               # print  permission                 &NBSP;&NBSP;&NBSP;&NBSP;EXPORTED_ACTIVITY.__SET_PERMISSION__ (permission)                  exported_activities.append (exported_activity) #  save result to xml filedef __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 ', &NBSP;ACTIVITY.__GET_PACKAGE_NAME__ ())         root.append (node)   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF&NBSP;ACTIVITY.__GET_PERMISSION__ ()  is not None:             node.set (' Permission ',  activity._ _GET_PERMISSION__ ())     tree.write ("Output.xml") # the activity is or  not mainactivitydef __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 funif  __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&NBSP;&NBSP;&NBSP;&NBSP;DEF&NBSP;__SET_PERMISSION__ (self, permission):        &nbsP;self.permission = permission    def __get_permission__ (self):         return self.permission




This article is from the "Wauoen" blog, make sure to keep this source http://7183397.blog.51cto.com/7173397/1829345

python--parsing androidmanifest.xml files using ElementTree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.