解析Android清单文件来寻找用途,许可标记,使用Python [英] Parsing Android Manifest File to look for the uses-permission tag using python
问题描述
我解析在Android应用程序中提到的用途,许可标记,在一个XML文件(AndroidManifest.xml中)
我曾尝试推行循环,使之反复,但我失败了,因此我在这里
Python的:
从xml.dom.minidom进口parseString
文件=打开('/根/桌面/ AndroidManifest.xml中','R')
数据= file.read()
file.close()
的dom = parseString(数据)
xmlTag = dom.getElementsByTagName('使用-权限')[0] .toxml()
打印xmlTag
输出:
<使用-权限的Android:名称=android.permission.INTERNET对>
< /使用-许可>
有关循环错误:
在xmlTag用途,权限:
#PRINT child.tag,child.attrib
打印xmlTag.tag
xmlTag = dom.getElementsByTagName('使用-许可')[1] .toxml()
xmlTag = dom._get_childNodes
#PRINT xmlTag
要找到所有的许可标签,尽量迭代的节点dom.getElementsByTagName(使用-权限')
返回而不是在指数 0
只访问节点:
从xml.dom.minidom进口parseString
数据=''
开放('/根/桌面/ AndroidManifest.xml中','R')为f:
数据= f.read()
的dom = parseString(数据)
节点= dom.getElementsByTagName('用途-许可)
#遍历所有的使用,允许节点
在节点的节点:
打印node.toxml()
如果你只想权限,而不是XML,你可以替换 node.toxml()
与 node.getAttribute(机器人:名称')
。
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
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')
.
这篇关于解析Android清单文件来寻找用途,许可标记,使用Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!