Zylva Zylva - 1 year ago 65
Python Question

Confused with XML Parsing in Python

Im Working on a little application for Android Development in Python and i need to Parse the AndroidManifest.xml to know the different activites and fetch their "roles" in the application , especially the Startup (LAUNCHER) activities
Here is an example of a dummy AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<activity android:name="com.vine.vox.MainActivity">
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>

<activity android:name="com.vine.vox.About">
<action android:name="android.intent.action.NOT_REALLY"/>
<category android:name="android.intent.category.GO_AHEAD"/>

<activity android:name="com.vine.vox.DeadStuff">
<action android:name="android.intent.action.DEAD"/>
<category android:name="android.intent.category.SOMETHING_DEAD"/>

What i need from this is to spot Which activity has
<category android:name="android.intent.category.LAUNCHER"/>

And the Location of the Activity from
<activity android:name="Activity Location">

In that example the Activity i want would be
<activity android:name="com.vine.vox.MainActivity">
since its the activity which has
<category android:name="android.intent.category.LAUNCHER"/>

I've Tried ElementTree , Minidom , LXML , But i still dont understand how it can really be done , i even tried to the old fashioned Text Parsing but i need the Tool to work on any AndroidManifest.xml the user wants and that came out to be a problem.

Sorry for the long post , any help i get is appreciated !


Answer Source

This can be done in a few lines using lxml and the following XPath :

    .//category/@android:name = 'android.intent.category.LAUNCHER'

The above XPath expression reads : find <activity> element containing <category> element where android:name attribute value equals 'android.intent.category.LAUNCHER', and then return android:name of the found activity.

Notice that you'll need to define prefix to namespace URI mapping to be able to use prefix (like 'android' in the following example) in your XPath :

from lxml import etree

tree = etree.parse("your_xml_file.xml")
ns = {'android' : 'http://schemas.android.com/apk/res/android'}

query = "//activity[.//category/@android:name = 'android.intent.category.LAUNCHER']/@android:name"

result = tree.xpath(query, namespaces=ns)

print result[0]

output :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download