2013-02-27 70 views
0

我想从Android项目中解析AndroidManifest.xml文件,以便在编译时以编程方式更改其某些值。 (使用xml.dom.minidom)破坏AndroidManifest.xml的Python XML分析器

不幸的是,解析器吐出畸形XML(即使我不做任何更改,从XML输入构建的DOM)

这是一个非常简单的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> 

这里是我的,只是加载文件非常简单的Python脚本,解析它,然后写回了:在运行SC后不幸

#!/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()) 

RIPT在AndroidManifest上述共享,下面的输出被写入磁盘:

<?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> 

这是怎么回事?是否有更好的方法来解析Python中的AndroidManifest?

编辑:我应该补充说,格式不正确的部分是第一个标签后的所有内容。

+0

你从哪里看到该文件的读/写语法? – Blender 2013-02-27 01:49:42

+0

SO的其他地方。 – NPike 2013-02-27 01:58:38

+0

为什么使用'os.open'而不是'open'? – 2013-02-27 02:10:54

回答

0

看起来像这是因为输出比输入短,所以它不会覆盖整个事情。

试图写出到另一个文件?

+0

这似乎是正确的。它写入新文件,但不会先清除它。 – NPike 2013-02-27 02:10:28

+2

您是否期望它? 'os.open'是相当低级的。通常你只需使用'open()'函数,就像这样:open(“AndroidManifest.xml”,'wb')'。 'w'模式将创建或截断。 – 2013-02-27 02:12:33

1

如果你想解析与Python XML,只需使用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) 

的语法是可以理解的,它包含在标准库,和它的作品。

+1

您至少应该提及['xml.etree.ElementTree'](http://docs.python.org/2/library/xml.etree.elementtree.html),它具有相同的API,并附带标准库。 lxml做了更多的工作,但是OP并不清楚它需要什么。 – 2013-02-27 02:09:16

+0

这似乎工作很好(etree) - 希望我可以接受这两个答案! – NPike 2013-02-27 02:27:13

+0

我在我的示例AndroidManifest上尝试了您的示例(复制并粘贴),并且输出有两个“android:allowBackup”属性。 NPike 2013-02-27 02:32:43