2

lxmlを使用して、Androidxmlファイルのいくつかの属性値を変更したいと思います。

例えば:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <TextView android:id="@+id/version_label"
              android:layout_marginLeft="5sp"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="version"/>
</LinearLayout>

「android:text」の値を変更したいのですが、

textViewTagNode.attrib['android:text'] = "new value" 

うまくいかなかった、どちらも

textViewTagNode.set('android:text', 'new value')

動作する可能性があります。私が得るのは「無効な属性名u'android:text'」だけです

名前空間の問題であることは知っていますが、修正方法がわかりません。

どうもありがとう。

4

1 に答える 1

1

あなたの鍵'android:text''{http://schemas.android.com/apk/res/android}text'

In [23]: s = '''<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <TextView android:id="@+id/version_label"
              android:layout_marginLeft="5sp"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="version"/> </LinearLayout>'''

In [24]: tree = etree.XML(s)                                          


In [25]: c = tree.getchildren()[0]

In [26]: c.items()
Out[26]:  [('{http://schemas.android.com/apk/res/android}id', '@+id/version_label'),  ('{http://schemas.android.com/apk/res/android}layout_marginLeft', '5sp'),  ('{http://schemas.android.com/apk/res/android}layout_width', 'fill_parent'),  ('{http://schemas.android.com/apk/res/android}layout_height', 'wrap_content'),  ('{http://schemas.android.com/apk/res/android}text', 'version')]
于 2012-11-09T03:08:10.427 に答える