// Xml Fiile
<Doc>
<Root>
<Record>
<ID>1</ID>
<name>Linea</name>
<rel>1</rel>
<last_update>0000-00-00 00:00:00</last_update>
</Record>
<Record>
<ID>2</ID>
<name>Vitigno</name>
<rel>2</rel>
<last_update>0000-00-00 00:00:00</last_update>
</Record>
<Record>
<ID>3</ID>
<name>Colore</name>
<rel>2</rel>
<last_update>0000-00-00 00:00:00</last_update>
</Record>
<Record>
<ID>4</ID>
<name>Profumo</name>
<rel>2</rel>
<last_update>0000-00-00 00:00:00</last_update>
</Record>
</Root>
</Doc>
// Calling Parsing class from Activity
public class Main extends Activity {
oncreate(Bundle icici)
{
ArrayList<CategoryWrapper> arrCategory = new ArrayList<CategoryWrapper>();
CategoryParser parser = new CategoryParser();
arrCategory = parser.ParseCategory();
// data will come in arrCategory
}
}
// Parsing class
public class CategoryParser extends DefaultHandler {
private ArrayList<CategoryWrapper> arrCategory = new ArrayList<CategoryWrapper>();
private CategoryWrapper categoryWrapper = null;
private String record;
public ArrayList<CategoryWrapper>ParseCategory()
{
try{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
URL url = new URL("insert your url");
sp.parse(url.openStream(), this);
} catch (Exception e) {
e.printStackTrace();
}
return arrCategory;
}
@Override
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
record = "";
if(localName.equalsIgnoreCase("ID")){
categoryWrapper = new CategoryWrapper();
}
}
@Override
public void endElement(String uri, String localName, String qName)throws SAXException {
super.endElement(uri, localName, qName);
if(localName.equalsIgnoreCase("ID"))
{
int id = Integer.parseInt(record);
categoryWrapper.setCategoryId(id);
}
else if(localName.equals("name"))
{
categoryWrapper.setCategoryName(record);
}
else if(localName.equalsIgnoreCase("rel"))
{
int rel = Integer.parseInt(record);
categoryWrapper.setRel(rel);
}
else if(localName.equalsIgnoreCase("last_update"))
{
}
else if(localName.equalsIgnoreCase("Record"))
{
arrCategory.add(categoryWrapper);
}
}
@Override
public void characters(char[] ch, int start, int length)throws SAXException {
super.characters(ch, start, length);
record += String.valueOf(ch,start,length);
}
}
// Category Wrapper
public class CategoryWrapper {
public int category_id;
public String category_name = null;
public int rel ;
public String category_TimeStamp;
public CategoryWrapper(){}
public void setCategoryId(int category_id){
this.category_id = category_id;
}
public int getCategory_id(){
return this.category_id;
}
public void setCategoryName(String category_name){
this.category_name = category_name;
}
public String getCategoryName(){
return this.category_name;
}
public void setRel(int rel){
this.rel = rel;
}
public int getRel(){
return this.rel;
}
public void setCategoryTimeStamp(String timestamp){
this.category_TimeStamp = timestamp;
}
public String getTimestamp(){
return category_TimeStamp;
}
}
<Doc>
<Root>
<Record>
<ID>1</ID>
<name>Linea</name>
<rel>1</rel>
<last_update>0000-00-00 00:00:00</last_update>
</Record>
<Record>
<ID>2</ID>
<name>Vitigno</name>
<rel>2</rel>
<last_update>0000-00-00 00:00:00</last_update>
</Record>
<Record>
<ID>3</ID>
<name>Colore</name>
<rel>2</rel>
<last_update>0000-00-00 00:00:00</last_update>
</Record>
<Record>
<ID>4</ID>
<name>Profumo</name>
<rel>2</rel>
<last_update>0000-00-00 00:00:00</last_update>
</Record>
</Root>
</Doc>
// Calling Parsing class from Activity
public class Main extends Activity {
oncreate(Bundle icici)
{
ArrayList<CategoryWrapper> arrCategory = new ArrayList<CategoryWrapper>();
CategoryParser parser = new CategoryParser();
arrCategory = parser.ParseCategory();
// data will come in arrCategory
}
}
// Parsing class
public class CategoryParser extends DefaultHandler {
private ArrayList<CategoryWrapper> arrCategory = new ArrayList<CategoryWrapper>();
private CategoryWrapper categoryWrapper = null;
private String record;
public ArrayList<CategoryWrapper>ParseCategory()
{
try{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
URL url = new URL("insert your url");
sp.parse(url.openStream(), this);
} catch (Exception e) {
e.printStackTrace();
}
return arrCategory;
}
@Override
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {
record = "";
if(localName.equalsIgnoreCase("ID")){
categoryWrapper = new CategoryWrapper();
}
}
@Override
public void endElement(String uri, String localName, String qName)throws SAXException {
super.endElement(uri, localName, qName);
if(localName.equalsIgnoreCase("ID"))
{
int id = Integer.parseInt(record);
categoryWrapper.setCategoryId(id);
}
else if(localName.equals("name"))
{
categoryWrapper.setCategoryName(record);
}
else if(localName.equalsIgnoreCase("rel"))
{
int rel = Integer.parseInt(record);
categoryWrapper.setRel(rel);
}
else if(localName.equalsIgnoreCase("last_update"))
{
}
else if(localName.equalsIgnoreCase("Record"))
{
arrCategory.add(categoryWrapper);
}
}
@Override
public void characters(char[] ch, int start, int length)throws SAXException {
super.characters(ch, start, length);
record += String.valueOf(ch,start,length);
}
}
// Category Wrapper
public class CategoryWrapper {
public int category_id;
public String category_name = null;
public int rel ;
public String category_TimeStamp;
public CategoryWrapper(){}
public void setCategoryId(int category_id){
this.category_id = category_id;
}
public int getCategory_id(){
return this.category_id;
}
public void setCategoryName(String category_name){
this.category_name = category_name;
}
public String getCategoryName(){
return this.category_name;
}
public void setRel(int rel){
this.rel = rel;
}
public int getRel(){
return this.rel;
}
public void setCategoryTimeStamp(String timestamp){
this.category_TimeStamp = timestamp;
}
public String getTimestamp(){
return category_TimeStamp;
}
}