2012-12-11 113 views
2

我正在尋找更新xml文件屬性值的方法。例如,下面的XML我想用另一個值替換android:versionCode30003的屬性。我很難理解ant如何使用replace或regex來做到這一點。如何替換xml屬性值?

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.app.see" 
    android:installLocation="auto" 
    android:versionCode="30003" 
    android:versionName="@string/app_version" > 
</manifest> 

回答

4

如果真的是這麼簡單,你可以使用replaceregexp

<property name="newVersionCode" value="30004"/> 
<replaceregexp file="${src}/AndroidManifest.xml" 
       match='(android:versionCode=").*(")' 
       replace="\1${newVersionCode}\2" 
       byline="true" 
/> 

否則,應考慮使用XSLT task。您可能希望將原始文件複製到臨時目錄中,然後應用將新值指定爲參數的樣式表,並通過原始清單生成輸出。

+0

如果整個xml標籤位於單行中,它刪除下一個屬性(ex versionName)。我們有其他方法可以解決嗎? – yokks

+0

要麼收緊正則表達式,要麼尋找使用更強大的查找和替換實現,比如XSLT任務。 –