How to parse the xml file in vim?

advertisements

I am currently working on an android project. And now i wish to complete a job: Parsing a xml file called AndroidManifest.xml and get some attribute from it. And set to 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 in <activity android:name="HistoryMainActivity". Then i can use this two value to compose a command to launch my app automatically. I just set these values by hand. But i think if vim can parse this file and set these values automatically, it's must very cool.


Consider xmlstarlet, xmllint --xpath

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

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