players.xml
------------
<?xml version="1.0" encoding="UTF-8"?>
<players>
<player>
<id>1</id>
<name>SACHIN</name>
<salary>50000</salary>
</player>
<player>
<id>2</id>
<name>KUNDAN</name>
<salary>60000</salary>
</player>
</players>
player.java
-------------
package com.example.xmlpullparsing;
public class Player {
private int id;
private String name;
private float salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
@Override
public String toString() {
return " Id= "+id + "\n Name= " + name + "\n Salary= " + salary;
}
}
XmlPullParserHandler .java
--------------------------------
package com.example.xmlpullparsing;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
public class XmlPullParserHandler {
private List<Player> players= new ArrayList<Player>();
private Player player;
private String text;
public List<Player> getPlayers() {
return players;
}
public List<Player> parse(InputStream is) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("player")) {
// create a new instance of employee
player = new Player();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("player")) {
// add employee object to list
players.add(player);
}else if (tagname.equalsIgnoreCase("id")) {
player.setId(Integer.parseInt(text));
} else if (tagname.equalsIgnoreCase("name")) {
player.setName(text);
} else if (tagname.equalsIgnoreCase("salary")) {
player.setSalary(Float.parseFloat(text));
}
break;
default:
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return players;
}
}
MainActivity .java
-------------------
package com.example.xmlpullparsing;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
List<Player> players = null;
try {
XmlPullParserHandler parser = new XmlPullParserHandler();
InputStream is=getAssets().open("players.xml");
players = parser.parse(is);
ArrayAdapter<Player> adapter =new ArrayAdapter<Player>(this,android.R.layout.simple_list_item_1, players);
listView.setAdapter(adapter);
} catch (IOException e) {e.printStackTrace();}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
------------
<?xml version="1.0" encoding="UTF-8"?>
<players>
<player>
<id>1</id>
<name>SACHIN</name>
<salary>50000</salary>
</player>
<player>
<id>2</id>
<name>KUNDAN</name>
<salary>60000</salary>
</player>
</players>
player.java
-------------
package com.example.xmlpullparsing;
public class Player {
private int id;
private String name;
private float salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
@Override
public String toString() {
return " Id= "+id + "\n Name= " + name + "\n Salary= " + salary;
}
}
XmlPullParserHandler .java
--------------------------------
package com.example.xmlpullparsing;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
public class XmlPullParserHandler {
private List<Player> players= new ArrayList<Player>();
private Player player;
private String text;
public List<Player> getPlayers() {
return players;
}
public List<Player> parse(InputStream is) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("player")) {
// create a new instance of employee
player = new Player();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("player")) {
// add employee object to list
players.add(player);
}else if (tagname.equalsIgnoreCase("id")) {
player.setId(Integer.parseInt(text));
} else if (tagname.equalsIgnoreCase("name")) {
player.setName(text);
} else if (tagname.equalsIgnoreCase("salary")) {
player.setSalary(Float.parseFloat(text));
}
break;
default:
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return players;
}
}
MainActivity .java
-------------------
package com.example.xmlpullparsing;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
List<Player> players = null;
try {
XmlPullParserHandler parser = new XmlPullParserHandler();
InputStream is=getAssets().open("players.xml");
players = parser.parse(is);
ArrayAdapter<Player> adapter =new ArrayAdapter<Player>(this,android.R.layout.simple_list_item_1, players);
listView.setAdapter(adapter);
} catch (IOException e) {e.printStackTrace();}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml
-------------------
<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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#FFCC00"
android:dividerHeight="4px"
>
</ListView>
</RelativeLayout>
No comments:
Post a Comment