I'm developing an iOS app in which I need to parse HTML from a link with the swiftsoup library. I have done it. But it shows all the table data as a string. I need to get separate data which should be stored in separate arrays.
Here is the table:
<table width="880" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="81"><strong>Trip Name </strong></td>
<td width="159"><div align="center"><strong>Starting Time from Campus </strong></div></td>
<td width="186"><div align="center"><strong>Starting Spot & Time </strong></div></td>
<td width="444"><strong>Remarks</strong></td>
</tr>
<tr>
<td><div align="center">Normal-1</div></td>
<td><div align="center">6:30 AM </div></td>
<td>Rupsha, 7:20 AM </td>
<td>Will back via Royalmore & Ferighat </td>
</tr>
<tr>
<td><div align="center">Normal-1</div></td>
<td><div align="center">6:45 AM </div></td>
<td>Moylapota, 7:25 AM </td>
<td>Will back via Shibbari - Sonadangha </td>
</tr>
</table>
And I have done to parse a string like Trip Name Starting Time from Campus Starting Spot & Time Remarks Normal-1
The code I've used :
let doc: Document = try! SwiftSoup.parse(html)
for element: Element in try! doc.select("table[width=880]")
{
let linkText : String = try! element.text();
print(linkText)
}
The Normal-1, 6:30AM, 7:20AM , Will back via Royalmore & Ferighat will be stored 4 separate array.
I'm not sure whether you want to store it as one-array-per-row or one-array-per-column. Here's how you can store it in one-array-per-row style. Use map
or other array transformations to convert it ot the style you want:
var tableContent = [[String]]()
let document = try! SwiftSoup.parse(html)
for row in try! document.select("table[width=\"880\"] tr") {
var rowContent = [String]()
for col in try! row.select("td") {
let colContent = try! col.text()
rowContent.append(colContent)
}
tableContent.append(rowContent)
}
print(tableContent)
(If you do this in production, handle errors properly instead of all those try!
)