Reputation: 13254
I am trying to parse the AndroidManifest.xml file from an Android project in order to programmatically change some of its values at build time. (using xml.dom.minidom)
Unfortunately the parser spits out malformed XML (even if I make no changes to the DOM constructed from the XML input)
Here is a very simple AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.npike.android.sampleapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="net.npike.android.sampleapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And here is my very simple python script that just loads the file, parses it, and then writes it back out:
#!/usr/bin/python
import os
from xml.dom.minidom import parse
dom1 = parse("AndroidManifest.xml")
f = os.open("AndroidManifest.xml", os.O_RDWR)
os.write( f, dom1.toxml() )
Unfortunately after running the script on the AndroidManifest shared above, the following output is written to disk:
<?xml version="1.0" ?><manifest android:versionCode="1" android:versionName="1.0" package="net.npike.android.sampleapp" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17"/>
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:label="@string/app_name" android:name="net.npike.android.sampleapp.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>tent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
What's going on here? Are there better approaches to parsing the AndroidManifest in Python?
EDIT: I should probably add that the malformed part is everything after the first tag.
Upvotes: 0
Views: 3193
Reputation: 298176
If you want to parse XML with Python, just use etree
:
import xml.etree.ElementTree as etree
etree.register_namespace('android', 'http://schemas.android.com/apk/res/android')
with open('AndroidManifest.xml', 'r') as handle:
root = etree.parse(handle)
root.find('application').set('android:allowBackup', 'false')
root.write('parsed.xml', encoding='utf-8', xml_declaration=True)
The syntax is understandable, it's included in the standard library, and it works.
Upvotes: 1
Reputation: 6409
Looks like this is because the output is shorter than the input so it's not overwriting the whole thing.
Tried writing out to another file?
Upvotes: 0