我正在开发一个移动应用程序,该应用程序允许用户导入html文件并将其保存在我的数据库中。现在,我一直在研究如何将html文件从文件管理器导入后解析为字符串。我使用了文件选择器,现在可以从手机文件中选择html。
如何将html解析为字符串?
我实际上找到了一些答案,但是所有答案都没有帮助我。
此代码是我从文件管理器中选择文件的地方。我在互联网上找到了一些答案,并在代码中应用了它,但仍然不允许我将解析的文件显示到.xml中。我想我没有正确使用选择的文件。请帮助我。
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();
}
}
}
}
答案 0 :(得分:0)
使用jsoup库,您可以轻松解析HTML文件的内容,这里的链接可以帮助您 https://medium.com/@ssaurel/learn-to-parse-html-pages-on-android-with-jsoup-2a9b0da0096f