如何从xml文件中读取数据,并将条目添加到ArrayList中

时间:2017-08-21 11:59:19

标签: java android xml arraylist

我的Android应用程序中有ArrayList和书籍,我在MainActivity中添加了100项,如下所示:

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

但是我不喜欢这样的事实,因为在MainActivity.java中,有很多行。我想将所有行移动到XML文件。但我不知道如何实现xml文件本身并从中读取数据。

1 个答案:

答案 0 :(得分:1)

你需要在下面的代码中进行XMLPullParsing,请检查

XML文件

<?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>

模式类

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;  
    }  
}  

此处public List<Employee> parse(InputStream is)此方法会返回您案例中的员工列表,这是一本书列表以获取更多信息,请参阅此链接https://www.javatpoint.com/android-XMLPullParser-tutorial