0

蟒蛇2.6

有萨克斯管

class MyHandler(sax.ContentHandler):
    def startElement(self, name, attrs):
        self.value = attrs['value']

它不起作用,因为在 xml 中,值被定义为 myscheme:value 如何摆脱 myscheme?实际上,我不知道我的脚本中会出现什么方案名称。我可以用 "myscheme:value".split(":") 拆分它,但我不喜欢它。怎么做才对?

我尝试获取清单节点的值。我需要 minSdkVersion maxSdkVersion 和 targetSdkVersion

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
                package="com.wnc_21.ci"
                android:versionCode="1"
                android:versionName="1.0">
                    <uses-sdk android:minSdkVersion="4"
                        android:targetSdkVersion="14"
                        android:maxSdkVersion="18"/>

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

                    <application
                        android:icon="@drawable/ic_launcher"
                        android:label="@string/app_name"
                        android:name=".mediator.ScoutCamApplication">

                        <activity
                            android:name=".SplashActivity"
                            android:label="@string/app_name"
                            android:screenOrientation="portrait">
                            <intent-filter>
                                <action android:name="android.intent.action.MAIN" />
                                <category android:name="android.intent.category.LAUNCHER" />
                            </intent-filter>
                        </activity>

                    </application>
                </manifest>


class Manifest(sax.ContentHandler):

def __init__(self, xml_path):
    sax.handler.ContentHandler.__init__(self)
    self.xml_path = xml_path

    self._package = None
    self._version_code = None
    self._version_name = None
    self._min_sdk_version = None
    self._target_sdk_version = None
    self._max_sdk_version = None

def parse(self):
    parser = sax.make_parser()
    parser.setContentHandler(self)
    parser.parse(self.xml_path)

def startElement(self, name, attrs):
    if 'manifest' == name:
        self.handle_manifest_node(attrs)
    elif 'uses-sdk' == name:
        self.handle_uses_sdk_node(attrs)

def endDocument(self):
    if self._package is None:
        raise ManifestException()

def handle_manifest_node(self, attrs):
    self._package = attrs.get('package')
    self._version_code = attrs.get('versionName')
    self._version_name = attrs.get('versionCode')

def handle_uses_sdk_node(self, attrs):
    self._max_sdk_version = attrs.get('maxSdkVersion')
    self._target_sdk_version = attrs.get('minSdkVersion')
    self._min_sdk_version = attrs.get('minSdkVersion')
4

1 回答 1

0

看起来您可以使用dom.minidom.

from xml.dom import minidom
minidom.parse('yourfile.xml')
data = {'min':None, 'max': None, 'target': None}
tag = doc.getElementsByTagName('uses-sdk')[0]
for i in range(e.attributes.length):
    for key in data:
        if key in e.attributes.item(i).name:
            data[key] = e.attributes.item(i).value

在我的机器上:

>>> for i in range(e.attributes.length):
...  for key in data:
...   if key in e.attributes.item(i).name:
...    data[key] = e.attributes.item(i).value
...
>>> data
{'target': '14', 'max': '18', 'min': '4'}
于 2013-10-14T21:49:54.890 回答