xml = """<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.wearable.timer" >
<uses-sdk android:minSdkVersion="20"
android:targetSdkVersion="22" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault.Light"
android:allowBackup="true">
</application>
</manifest>"""
要設置的值,查找節點然後訪問那麼它的ATTRIB字典使用QName,其中第一個參數是命名空間URI,第二個是屬性名稱設置它的屬性的新值:
import lxml.etree as et
tree = et.fromstring(xml)
# holds namespace mappings
nsmap = tree.nsmap
# get prefix URI
android = tree.nsmap["android"]
# find app and set the attribute value.
tree.find("application", nsmap).attrib[et.QName(android, "allowBackup")] = "false"
tree.find("uses-sdk", nsmap).attrib[et.QName(android, "minSdkVersion")] = "17"
print(et.tostring(tree, pretty_print=True))
它給你:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.wearable.timer">
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="22"/>
<application android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.DeviceDefault.Light" android:allowBackup="false">
</application>
</manifest>
讀取和並寫入文件,邏輯僅僅是在同一最終你write傳遞文件名和任何其他ARGS:
new.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.wearable.timer">
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="22"/>
<application android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.DeviceDefault.Light" android:allowBackup="false">
</application>
</manifest>
現在你知道怎麼寫的變化,作爲缺少的屬性原樣的作品,如果值存在我們更新它,如果不是我們創建它:
# no minSDk...
In [31]: !cat test.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.wearable.timer" >
<uses-sdk android:targetSdkVersion="22" />
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault.Light"
android:allowBackup="true">
</application>
</manifest>
In [32]: tree = et.parse("test.xml")
In [33]: root = tree.getroot()
In [34]: nsmap = root.nsmap
In [35]: android = nsmap["android"]
In [36]: tree.find("uses-sdk", nsmap).attrib[et.QName(android, "minSdkVersion")] = "17"
In [37]: tree.write("test.xml", encoding="utf-8")
# New attribute and value created.
In [38]: !cat test.xml<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.wearable.timer">
<uses-sdk android:targetSdkVersion="22" android:minSdkVersion="17"/>
<application android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.DeviceDefault.Light" android:allowBackup="true">
</application>
</manifest>
In [39]: