假设我有一个像这样订购的网站:
<div id = "main">
<div class = "cat"
<div class = "cat_top"
<h3>Math Class</h3>
</div>
<ul>
<li class = "student_male">
<span class = "name">Buddy</span>
<span class = "age">22</span>
</li>
<li class = "student_female">
<span class = "name">Kristy</span>
<span class = "age">24</span>
</li>
</ul>
</div>
到目前为止,我一直这样做:
ArrayList<String> studentList = new ArrayList();
Element main = doc1.select("div#main").first();
for(Element e : main.select("div.cat")){
System.out.println(e.text());
}
我想将所有文本添加到ArrayList中,但我无法弄清楚如何将它分开以便它是这样的:
Cat1:
Name: Buddy
Age: 22
Name: Kristy
Age: 24
Cat2:
Name: Student
Age:23
Name: Student
Age: 25
我只是不确定如何遍历这些类别,以便它在我的数组列表中格式化,以便它可读而不是一个长列表。也许我需要多个ArrayLists,我不确定因为我是Java的新手。