如何处理没有命名空间的xml attrs

时间:2013-10-14 21:05:26

标签: python

python 2.6

有萨克斯处理程序

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

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

我尝试获取manifest节点的值。我需要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')

1 个答案:

答案 0 :(得分: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'}