Introduction
In this article, you will learn about XML Parsing using the DOM parser.
SDomParser
The DOM Parser is used to convert data from a server into a machine-readable format.
Step 1
Create an XML File and write this
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MainActivity">
- <ListView android:id="@android:id/list"
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- />
-
- </RelativeLayout>
Step 2
Create a Java File and write the following.
In this first we the string to the HTTP Client to get the stream from the server. And by using this Stream you will get the DOM element. Now you will apply parsing.
Step 3
Android Manifest.Xml file:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.xmlparsingdomparser"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="7"
- android:targetSdkVersion="16" />
- <uses-permission android:name="android.permission.INTERNET"/>
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.xmlparsingdomparser.MainActivity
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
Step 4
Output
See the values in logcat: