-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyse_xml_file.py
67 lines (43 loc) · 1.71 KB
/
analyse_xml_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import xml
'''
用于活动图生成,通过解析androidmanifest文件实现
'''
# 解析目录/res/strings下的文件,得到layout和key的对
def get_layout_pairs(res_string_path):
layout_pairs = {}
if not os.path.exists(res_string_path):
return layout_pairs
try:
dom = xml.dom.minidom.parse(res_string_path)
except:
return layout_pairs
root = dom.documentElement
public_list = root.getElementsByTagName('public')
for public_element in public_list:
if public_element.getAttribute('type') == 'layout':
layout_pairs[public_element.getAttribute('id')] = public_element.getAttribute('name')
return layout_pairs
# 解析AndroidManifest文件,得到程序的mainactivity
def get_Main_activity(AndroidManifest_path):
main_activity = ''
if not os.path.exists(AndroidManifest_path):
return main_activity
try:
dom = xml.dom.minidom.parse(AndroidManifest_path)
except:
return main_activity
root = dom.documentElement
activity_list = root.getElementsByTagName('activity')
pakage = root.getAttribute('pakage')
# 遍历所有的activity
for activity in activity_list:
# 满足mainactivity的条件
if activity.toxml().find('android.intent.action.MAIN') > 0 and activity.toxml().find('android.intent.category.LAUNCHER') > 0:
main_activity = activity.getAttribute('android:name')
# 加上包名作为键,防止重名
if main_activity.startswith('.'):
main_activity = pakage + main_activity
if not '.' in main_activity:
main_activity = pakage + '.' + main_activity
return main_activity