1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to parse several xml files at the same time and display them with their data in a listview

Discussion in 'Android Development' started by LefmyHans, Sep 20, 2019.

  1. LefmyHans

    LefmyHans Lurker
    Thread Starter

    Hello I would parser several xml files and display their data each in a list. I manage to parse a single file. The code is just below. How to parse multiple files in a folder of external memory and display their data in a list?

    this is my MainActivity

    public List<ModelKmlData> modelkmldata = null;

    private View parent_view;

    private RecyclerView recyclerView;
    private AdapterListInbox mAdapter;
    private androidx.appcompat.view.ActionMode actionMode;
    private Toolbar toolbar;
    private FloatingActionButton fabbtn;
    SqliteHelper sqliteHelper;

    public List<KmlDataModel> kmldatamodel = null;

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps_data);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    if (ActivityCompat.checkSelfPermission(this,
    Manifest.permission.WRITE_EXTERNAL_STORAGE)
    == PackageManager.PERMISSION_GRANTED){

    // Analyser les données KML dans un thread non-ui
    new LoadKmlTask().execute();

    }


    else{

    ActivityCompat.requestPermissions(this, new String[]
    {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }
    }
    else{
    //startActivity(new Intent(getActivity(),
    SQLite2ExcelActivity.class));
    }

    /*sqliteHelper = new SqliteHelper(this);

    initToolbar();
    initComponent();
    Toast.makeText(this, "Appui long effectué",
    Toast.LENGTH_SHORT).show();*/
    }

    private class LoadKmlTask extends AsyncTask<String, Void, Cursor>{
    @override
    protected Cursor doInBackground(String... args) {

    // interroger la base de données et retourner un curseur des
    //données kml.
    SqliteHelper sqliteHelper = new
    SqliteHelper(getApplicationContext());

    Cursor cursor = sqliteHelper.getAllKmlCursor();

    return cursor;
    }

    @override
    protected void onPostExecute(Cursor cursor) {

    String[] dataColumns = { sqliteHelper.COLUMN_KMLNAME,
    sqliteHelper.COLUMN_KMLUNID, sqliteHelper.COLUMN_KMLDESC,
    sqliteHelper.COLUMN_KMLCOORD};
    int[] viewIDs = { R.id.list_item_name, R.id.list_item_title,
    R.id.list_item_department,R.id.list_item_email };

    SimpleCursorAdapter records = new
    SimpleCursorAdapter(getBaseContext(), R.layout.item_inbox,
    cursor, dataColumns, viewIDs, 0);

    ListView listView = (ListView) findViewById(R.id.list);
    if (listView != null) {
    listView.setAdapter(records);
    }

    }

    }

    the code that parses :

    // noms des balises KML
    private static final String PLACEMARK = "Placemark";
    private static final String DOCUMENT = "Document";
    private static final String VALUE = "value";
    private static final String NAME = "name";
    private static final String DESCRIPTION = "description";
    private static final String COORDINATES = "coordinates";
    private ArrayList<ModelKmlData> kmlList = null;
    private ModelKmlData currentKml = null;
    private boolean done = false;
    private String currentTag = null;

    public ArrayList<ModelKmlData> parse(Context context) {

    try{
    String state = Environment.getExternalStorageState();
    String path = Environment.getExternalStorageDirectory()
    + File.separator + "Fodecc_csv" + File.separator + "GFAM-
    20190916-150100.kml";
    XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
    xppf.setNamespaceAware(true);
    XmlPullParser parser = xppf.newPullParser();

    File file = new File(Environment.getExternalStorageDirectory()
    + File.separator + "Fodecc_csv" + File.separator + "GFAM-
    20190916-150100.kml");
    FileInputStream fis = new FileInputStream(file);
    parser.setInput(new InputStreamReader(fis));


    //XmlPullParser parser = fis;

    int eventType = parser.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT && !done) {

    switch (eventType) {
    case XmlPullParser.START_DOCUMENT:
    kmlList = new ArrayList<ModelKmlData>();
    break;
    case XmlPullParser.START_TAG:
    currentTag = parser.getName();
    if (currentTag.equalsIgnoreCase(PLACEMARK)) {
    currentKml = new ModelKmlData();
    } else if (currentKml != null) {
    if (currentTag.equalsIgnoreCase(NAME)) {
    currentKml.set_kmlName(parser.nextText());
    // currentEmployee.setId(parser.nextText());
    } else if (currentTag.equalsIgnoreCase(VALUE)) {
    currentKml.set_kmlUNID(parser.nextText());
    } else if
    (currentTag.equalsIgnoreCase(DESCRIPTION)) {
    currentKml.set_kmlDesc(parser.nextText());
    } else if
    (currentTag.equalsIgnoreCase(COORDINATES)) {
    currentKml.set_kmlCoord(parser.nextText());
    }
    }
    break;
    case XmlPullParser.END_TAG:
    currentTag = parser.getName();
    if (currentTag.equalsIgnoreCase(PLACEMARK) &&
    currentKml != null) {
    kmlList.add(currentKml);
    } else if (currentTag.equalsIgnoreCase(DOCUMENT)) {
    done = true;
    }
    break;
    }
    eventType = parser.next();
    }
    } catch (XmlPullParserException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    return kmlList;
    }
    }

    How could I add a loop that will recover all files and view their contents ?
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...