Android
 Computer >> コンピューター >  >> プログラミング >> Android

XMLPullParserを使用してKotlinを使用してAndroidでXMLを解析するにはどうすればよいですか?


この例は、XMLPullParserを使用して、Kotlinを使用してAndroidでXMLを解析する方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">
   <ListView
      android:id="@+id/listView"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:dividerHeight="1dp" />
</LinearLayout>

ステップ3 −レイアウトリソースファイル(row.xml)を作成し、次のコードを追加します-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="4dp">
   <TextView
      android:id="@+id/tvName"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textColor="@android:color/background_dark"
      android:textSize="16sp"
      android:textStyle="bold" />
   <TextView
      android:id="@+id/tvDesignation"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/tvName"
      android:layout_marginTop="7dp"
      android:textColor="#343434"
      android:textSize="12sp" />
   <TextView
      android:id="@+id/tvLocation"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBaseline="@+id/tvDesignation"
      android:layout_alignBottom="@+id/tvDesignation"
      android:layout_alignParentEnd="true"
      android:textColor="@android:color/background_dark"
      android:textSize="16sp" />
</RelativeLayout>

ステップ5 −新しいアセットフォルダを作成し、アセットフォルダ内にAndroidリソースファイル(model.xml)を作成して、次のコードを追加します-

<?xml version="1.0" encoding="utf-8"?>
<resources>
<users>
   <user>
      <name>Sehwag</name>
      <designation>Vice Captain</designation>
      <loation>Delhi</loation>
   </user>
   <user>
      <name>Ashwin</name>
      <designation>Off Spin Bowler</designation>
      <loation>Chennai</loation>
   </user>
   <user>
      <name>Dhoni</name>
      <designation>Captain</designation>
      <loation>Ranchi</loation>
   </user>
</users>
</resources>

ステップ6 −次のコードをsrc / MainActivity.kt

に追加します
import android.os.Bundle
import android.widget.ListAdapter
import android.widget.ListView
import android.widget.SimpleAdapter
import androidx.appcompat.app.AppCompatActivity
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException
import org.xmlpull.v1.XmlPullParserFactory
import java.io.IOException
class MainActivity : AppCompatActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      try {
         val userList = ArrayList<java.util.HashMap<String?, String?>>()
         var user: HashMap<String?, String?>? = HashMap()
         val lv: ListView = findViewById(R.id.listView)
         val inputStream = assets.open("model.xml")
         val parserFactory: XmlPullParserFactory = XmlPullParserFactory.newInstance()
         val parser: XmlPullParser = parserFactory.newPullParser()
         parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true)
         parser.setInput(inputStream, null)
         var tag: String?
         var text = ""
         var event = parser.eventType
         while (event != XmlPullParser.END_DOCUMENT) {
            tag = parser.name
            when (event) {
               XmlPullParser.START_TAG -> if (tag == "user") user = HashMap()
               XmlPullParser.TEXT −> text = parser.text
               XmlPullParser.END_TAG −> when (tag) {
                  "name" −> user!!["name"] = text
                  "designation" −> user!!["designation"] = text
                  "location" −> user!!["location"] = text
                  "user" −> if (user != null) userList.add(user)
               }
            }
            event = parser.next()
         }
         val adapter: ListAdapter = SimpleAdapter(this@MainActivity, userList, R.layout.row,
         arrayOf("name", "designation", "location"), intArrayOf(R.id.tvName,
         R.id.tvDesignation, R.id.tvLocation))
         lv.adapter = adapter
      } catch (e: IOException) {
         e.printStackTrace()
      } catch (e: XmlPullParserException) {
         e.printStackTrace()
      }
   }
}

ステップ6 −次のコードをandroidManifest.xmlに追加します

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、[実行]アイコンをクリックします ツールバーからXMLPullParserを使用してKotlinを使用してAndroidでXMLを解析するにはどうすればよいですか? 。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します

XMLPullParserを使用してKotlinを使用してAndroidでXMLを解析するにはどうすればよいですか?


  1. XMLPullParserを使用してAndroidでXMLを解析するにはどうすればよいですか?

    この例は、AndroidでXMLPullParserを実行する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://s

  2. ボレーライブラリを使用してAndroidアプリでJSONを解析するにはどうすればよいですか?

    この例は、ボレーライブラリを使用してAndroidアプリでJSONを解析する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="