首页 > 解决方案 > 如何在flutter中解析特定数据html

问题描述

我想从 html 文本中获取文件夹名称列表。

我从 api 获取 html,它看起来像这样:

<ul><li class='expanded' data-icon='folder_docs.gif' id='fol_1' onclick='getli(this.id,0)' dcr='7/26/2017 2:42:47 PM' spname='مستندات'>مستندات<ul><li class='' id='fol_20'onclick='getli(this.id,0)' f_pertype='2' dcr='7/5/2019 10:29:02 AM' is_Wr='True' own='13781' spname='test'> test<ul><li class='' id='fol_27'onclick='getli(this.id,0)' f_pertype='2' dcr='9/3/2020 9:10:26 AM' is_Wr='True' own='13781' spname='test2'> test2<ul><li class='' id='fol_29'onclick='getli(this.id,0)' f_pertype='2' dcr='9/15/2020 3:2…ue' own='13781' spname='test'> test</li><li class='' id='fol_36'onclick='getli(this.id,0)' f_pertype='2' dcr='12/7/2020 10:40:08 AM' is_Wr='True' own='13781' spname='file1'> file1</li><li class='' id='fol_37'onclick='getli(this.id,0)' f_pertype='2' dcr='12/14/2020 9:09:18 AM' is_Wr='True' own='13781' spname='test'> test</li><li class='' id='fol_38'onclick='getli(this.id,0)' f_pertype='2' dcr='12/23/2020 12:23:14 PM' is_Wr='True' own='13781' spname='createdfromMobile'> createdfromMobile</li></ul></li></ul>"

我想从此 html 中获取所有“spname”数据。

这就是我所做的:

var foldertree = parse(htmltext.toString());
List<String> data = [];
data.add(foldertree.getElementsByClassName("class")[0].innerHtml);
var temp  = foldertree.getElementsByClassName("expanded")[0];      
data.add(temp.innerHtml.substring(0, temp.innerHtml.indexOf("<li>")));           
data.add(temp.getElementsByTagName("spname")[0].innerHtml);                                           
print(data.toString);   

标签: htmljsonflutterdart

解决方案


var rows=foldertree.getElementsByTagName("ul")[0].getElementsByTagName("li");
var spnameList = rows.map((element) => element.attributes['spname']).toList();

推荐阅读