How to parse xml file in vim?

I am currently working on an Android project. And now I want to complete the task: analyze the xml file with the name AndroidManifest.xml and get some attribute from it. And set some values ​​in vim.

Here is the AndroidManifest.xml file.
 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.base.module.callhistory" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:label="@string/app_name" android:theme="@android:style/Theme.Light.NoTitleBar" android:icon="@drawable/call_history"> <activity android:name="HistoryMainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="HistoryListActivity"> <intent-filter> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name="DetailListActivity"> <intent-filter> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest> 

I want to parse package="com.base.module.callhistory" and HistoryListActivity into <activity android:name="HistoryMainActivity" . Then I can use these two values ​​to make a command to launch my application automatically. I just set these values ​​manually. But I think that if vim can analyze this file and set these values ​​automatically, it should be very cool.

+4
source share
2 answers

Consider xmlstarlet, xmllint --xpath

Otherwise, you can use perl or python to achieve your goal if you compiled it (usually in batch versions)

Regardless, you can use nomarl search ( / ) patterns with c i t to replace the contents of the tag

+1
source

I had the same requirement as you, and I just wrote a custom python script for this task.

0
source

Source: https://habr.com/ru/post/1398574/


All Articles