0

I am working on a mobile application that allows the user import an html file and save it in my db.. Now I've been stuck on how to parse the html file to string after importing it from my file manager. I used file picker and now I am able to pick the html from my phone files.
How do I parse that html to string?
I actually find some answers but all of them didn't help me. This code is where I pick file from my file manager. I find some answers in the internet and I applied it in my codes,but still it doesn't allow me to display the parsed file into my .xml . I guess I did not use the picked file correctly. please help me guys.

   private ProgressDialog mProgressDialog;
private ArrayList <String> sNameList=new ArrayList <>(  );
private ArrayList <String> sIdList=new ArrayList <>(  );
private ArrayList <String> sCourseList = new ArrayList <>(  );
Button btnImport;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_subject_class_list );

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M && checkSelfPermission( Manifest.permission.WRITE_EXTERNAL_STORAGE )
            != PackageManager.PERMISSION_GRANTED)
    {
        requestPermissions( new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1001);
    }

    Toolbar myToolbar =  findViewById( R.id.toolbarAct );
    setSupportActionBar( myToolbar );

    if (getSupportActionBar()!= null )
    {

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled( true );
    }

    getSupportActionBar().setTitle( "[CLASS NAME]" );

    btnImport = findViewById( R.id.btn_import );

    btnImport.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            new MaterialFilePicker()
                    .withActivity(subject_class_list.this)
                    .withRequestCode(1000)
                    .withHiddenFiles(true) // Show hidden files and folders
                    .start();
        }
    } );
}

//public void onBackPressed(){
//    moveTaskToBack( true );
//    super.onBackPressed();
//}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult( requestCode, resultCode, data );

    if (requestCode == 1000 && resultCode == RESULT_OK) {
        String filePath = data.getStringExtra( FilePickerActivity.RESULT_FILE_PATH );
        // Do anything with file

    doInBackground( filePath );

    }
}

protected Void doInBackground(String params)
{
    Document mfile = (Document) Jsoup.parse( params );
    Elements mElementsDataSize = (Elements) mfile.getElementsByTagName( "table[border=0 cellspacing=2]");

    int mElementSize = mElementsDataSize.size();
    for (int i = 0; i < mElementSize; i++)
    {
        Elements sNameLoadList = (Elements) mfile.getElementsByTagName( "a[title=Click here to view the student schedule]" );
        String StudentName = sNameLoadList.text();

       // Elements sIdList = (Elements) mfile.getElementsByTagName(  );
        //String StudentID = sIdList.text();

        //Elements sCourseList = (Elements) mfile.getElementsByTagName(  );
      //  String StudentCourse = sCourseList.text();

       sNameList.add( StudentName );
    }

    return null;
}

protected void onPostExecute(Void result)
{
    RecyclerView mRecyclerView = (RecyclerView)findViewById( R.id.act_recycleview );
    DataAdapter mDataAdapter = new DataAdapter( subject_class_list.this , sNameList );
    RecyclerView.LayoutManager mLayoutManager =new LinearLayoutManager( getApplicationContext() );
    mRecyclerView.setLayoutManager( mLayoutManager );
    mRecyclerView.setAdapter( mDataAdapter );
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode)
    {
        case 1001:
        {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                Toast.makeText( this, "Permission granted.", Toast.LENGTH_SHORT ).show();
            }
            else
            {
                Toast.makeText( this, "Permission not granted.", Toast.LENGTH_SHORT ).show();
            }
        }
    }
}
  • try to give a detailed information about your question. Add your code , what issue your facing? , where your blocking? , what you tried so far? like the way you have to ask your question. Please [Take the Tour](https://stackoverflow.com/tour) , and be sure to read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Agilanbu Nov 27 '18 at 07:31

1 Answers1

0

Using jsoup library you can parse the content of HTML file easily here is a link that helps you https://medium.com/@ssaurel/learn-to-parse-html-pages-on-android-with-jsoup-2a9b0da0096f

Zahoor Saleem
  • 614
  • 7
  • 15