-4

I have ArrayList with books in my android app, and I add 100 items in MainActivity as below:

a = new Book("J. K. Roaling", "Harry Potter", R.drawable.book_cover1);
bookList.add(a);

But I do not like the fact that because of this in MainActivity.java, there are a lot of lines. I would like to move all the lines to an XML file. But I do not know how to implement the xml file itself and read data from it.

1 Answers1

2

you need to do XMLPullParsing here below code, please check

XML File

<?xml version="1.0" encoding="UTF-8"?>  
<employees>  
    <employee>  
        <id>1</id>  
        <name>Sachin</name>  
        <salary>50000</salary>        
    </employee>  
    <employee>  
        <id>2</id>  
        <name>Nikhil</name>  
        <salary>60000</salary>    
    </employee>  
</employees>

Mode class

public class Employee {  
     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;  
    }  
}   

XMLPullParserHandler

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<Employee> employees= new ArrayList<Employee>();  
    private Employee employee;  
    private String text;  

    public List<Employee> getEmployees() {  
        return employees;  
    }  

    public List<Employee> 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("employee")) {  
                        // create a new instance of employee  
                        employee = new Employee();  
                    }  
                    break;  

                case XmlPullParser.TEXT:  
                    text = parser.getText();  
                    break;  

                case XmlPullParser.END_TAG:  
                    if (tagname.equalsIgnoreCase("employee")) {  
                        // add employee object to list  
                        employees.add(employee);  
                    }else if (tagname.equalsIgnoreCase("id")) {  
                        employee.setId(Integer.parseInt(text));  
                    }  else if (tagname.equalsIgnoreCase("name")) {  
                        employee.setName(text);  
                    } else if (tagname.equalsIgnoreCase("salary")) {  
                        employee.setSalary(Float.parseFloat(text));  
                    }   
                    break;  

                default:  
                    break;  
                }  
                eventType = parser.next();  
            }  

        } catch (XmlPullParserException e) {e.printStackTrace();}   
        catch (IOException e) {e.printStackTrace();}  

        return employees;  
    }  
}  

here public List<Employee> parse(InputStream is) this method returns the list of employees in your case this is a list of book for more information refer this link https://www.javatpoint.com/android-XMLPullParser-tutorial

Kishan Donga
  • 2,851
  • 2
  • 23
  • 35