raw->books.xml
------------------
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<bid>B-01</bid>
<bname>master android</bname>
<auther>kk</auther>
<price>99</price>
</book>
<book>
<bid>B-02</bid>
<bname>android cook book</bname>
<auther>kundan</auther>
<price>900</price>
</book>
</books>
avtivity_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/book_details"
android:textColor="#999999"
android:textSize="30dp" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="16dp" >
</ListView>
</RelativeLayout>
item.xml
----------------
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
MainActivity.java
---------------------
package com.example.xmlparsing;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
List<String>booksinfo=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
XMLReader reader=parser.getXMLReader();
BooksHandler handler=new BooksHandler();
reader.setContentHandler(handler);
InputStream is=getResources().openRawResource(R.raw.books);
reader.parse(new InputSource(is));
booksinfo=handler.getBooksInfo();
}catch(Exception e){
e.printStackTrace();
}
ArrayAdapter<String>bookList=new ArrayAdapter<String>(this,R.layout.item,booksinfo);
setListAdapter(bookList);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
------------------
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<bid>B-01</bid>
<bname>master android</bname>
<auther>kk</auther>
<price>99</price>
</book>
<book>
<bid>B-02</bid>
<bname>android cook book</bname>
<auther>kundan</auther>
<price>900</price>
</book>
</books>
avtivity_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/book_details"
android:textColor="#999999"
android:textSize="30dp" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="16dp" >
</ListView>
</RelativeLayout>
item.xml
----------------
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
MainActivity.java
---------------------
package com.example.xmlparsing;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
List<String>booksinfo=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
XMLReader reader=parser.getXMLReader();
BooksHandler handler=new BooksHandler();
reader.setContentHandler(handler);
InputStream is=getResources().openRawResource(R.raw.books);
reader.parse(new InputSource(is));
booksinfo=handler.getBooksInfo();
}catch(Exception e){
e.printStackTrace();
}
ArrayAdapter<String>bookList=new ArrayAdapter<String>(this,R.layout.item,booksinfo);
setListAdapter(bookList);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
BooksHandler.java
----------------------
package com.example.xmlparsing;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
import org.xml.sax.helpers.DefaultHandler;
public class BooksHandler extends DefaultHandler {
List<String>list=new ArrayList<String>();
public void startDocument(){
System.out.println("startDocument()");
}
public void endDocument(){
System.out.println("endDocument()");
}
public void startElement(String tns,String tn,String tln,Attributes atts){
System.out.println("startElement()");
}
public void endElement(String tns,String tn,String tln){
System.out.println("endtElement()");
}
public void characters(char ch[],int start,int total){
System.out.println("characters()");
String str=new String(ch,start,total);
list.add(str);
}
public List<String> getBooksInfo(){
return list;
}
}
No comments:
Post a Comment