Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Android Manifest File to look for the uses-permission tag using python

I am parsing the uses-permission tag in an xml file(androidmanifest.xml) mentioned in an android application

I have tried implementing a for loop to make it iterative but i have failed and so am here

Python:

from xml.dom.minidom import parseString
file = open('/root/Desktop/AndroidManifest.xml','r')
data = file.read()
file.close()
dom = parseString(data)
  xmlTag = dom.getElementsByTagName('uses-permission')[0].toxml()

  print xmlTag

Output:

    <uses-permission android:name="android.permission.INTERNET">
</uses-permission>

for loop mistake:

for uses-permission in xmlTag:
    #print child.tag, child.attrib
    print xmlTag.tag
xmlTag = dom.getElementsByTagName('uses-permission')[1].toxml()
xmlTag= dom._get_childNodes
#print xmlTag
like image 868
user2819379 Avatar asked Sep 26 '13 12:09

user2819379


People also ask

How do I read manifest files on Android?

Just open your APK and in treeview select "AndroidManifest. xml". It will be readable just like that.

What is AndroidManifest xml in Android?

Every app project must have an AndroidManifest. xml file (with precisely that name) at the root of the project source set. The manifest file describes essential information about your app to the Android build tools, the Android operating system, and Google Play.


1 Answers

To find all the permission tags, try iterating over the nodes that dom.getElementsByTagName('uses-permission') returns instead of accessing only the node at index 0:

from xml.dom.minidom import parseString

data = ''
with open('/root/Desktop/AndroidManifest.xml','r') as f:
    data = f.read()
dom = parseString(data)
nodes = dom.getElementsByTagName('uses-permission')
# Iterate over all the uses-permission nodes
for node in nodes:
    print node.toxml()

or if you want just the permission and not the xml, you can replace node.toxml() with node.getAttribute('android:name').

like image 129
crennie Avatar answered Sep 19 '22 21:09

crennie