Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में कोटलिन का उपयोग करके XMLPullParser से XML कैसे पार्स करें

एंड्रॉइड में कोटलिन का उपयोग करके XMLPullParser से XML पार्स करना

यह उदाहरण दर्शाता है कि एंड्रॉइड में कोटलिन का उपयोग करके XMLPullParser से XML कैसे पार्स किया जाए।

चरण 1 - नया प्रोजेक्ट बनाएं

एंड्रॉइड स्टूडियो में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और सभी आवश्यक विवरण भरकर नया प्रोजेक्ट बनाएं।

चरण 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>

चरण 4 - एसेट फ़ोल्डर में model.xml बनाएं

एक नया एसेट फ़ोल्डर बनाएं, और उसके अंदर model.xml नामक एंड्रॉइड संसाधन फ़ाइल बनाएं:

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

चरण 5 - 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>

ऐप चलाएं

अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें। एंड्रॉइड स्टूडियो में प्रोजेक्ट की एक्टिविटी फ़ाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिवाइस पर डिफ़ॉल्ट स्क्रीन देखें।

  1. Android Toolbar में SearchView का उपयोग कैसे करें? Android Toolbar में SearchView का उपयोग कैसे करें?

    Android Toolbar/ActionBar में SearchView जोड़कर ListView फ़िल्टर करना टूलबार में सर्च आइकन जोड़कर ListView डेटा को रियल-टाइम फ़िल्टर करने का पूरा उदाहरण। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project। चरण 2: लेआउट में Toolbar और ListView res/layout/activity_main.xml: <?xml ve

  1. एंड्रॉइड में XMLPullParser से XML पार्स कैसे करें? एंड्रॉइड में XMLPullParser से XML पार्स कैसे करें?

    एंड्रॉइड में XMLPullParser का उपयोग करके XML डेटा पार्स करना XMLPullParser एंड्रॉइड में XML प्रोसेसिंग के लिए एक हल्का, कुशल और लो-मेमोरी पार्सर है। यह पुल मॉडल पर काम करता है — यानी आप पार्सर से अगला इवेंट मांगते हैं, बजाय इसके कि पार्सर आपको कॉलबैक दे (जैसे SAX)। यह बड़े XML फाइल्स के लिए आदर्श है

  1. Android ऐप में Volley लाइब्रेरी से JSON कैसे पार्स करें? Android ऐप में Volley लाइब्रेरी से JSON कैसे पार्स करें?

    Volley लाइब्रेरी से JSON पार्सिंग यह उदाहरण दर्शाता है कि Google की Volley लाइब्रेरी का उपयोग करके नेटवर्क से JSON डेटा कैसे प्राप्त करें और पार्स करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project से प्रोजेक्ट बनाएं। चरण 2: build.gradle में Volley डिपेंडेंसी जोड़ें dependencie