Sadly, the table is written in a single line in order to use code like FlickrViewer example, and I have no knowledge of the power/limits of Regex function.
Sub ParseTable(s As String)
'<td class="linksazul" align="left">Coronas Suecas</td>
'<td class="linksazul" align="center">63.4090</td>
'<td class="linksazul" align="center">64.4845</td>
'<td class="linksazul" align="center"></td>
Dim m As Matcher
m = Regex.Matcher( _
"<td class=~linksazul~ [^>]+>([^<]+)</td>[^>]+>([^<]+)</td>[^>]+>([^<]+)</td>".Replace("~", "\" & QUOTE), s)
Do While m.Find
Log("col1=" & m.Group(1))
Log("col2=" & m.Group(2))
Log("col3=" & m.Group(3))
Log("*********************")
Loop
End Sub
I have assimilate problem here. Trying to parse table from this link SIA.ba - mobile using regex but no success. Tried this :
B4X:
Sub Parse(html As String)
'trying to remove linebreaks
html = html.Replace(CRLF,"")
Dim m As Matcher
m = Regex.Matcher(table_pattern, html)
Do While m.Find
Log(m.GroupCount)
Log(m.Match)
Log("col1=" & m.Group(1))
Loop
'ABOVE CODE CAN'T FIND TABLE although there is just single table in a page
'but when using this sample table it works
'html= "asdasasd<table>something</table>asdadasd"
End If
End Sub
Here is a better approach based on the new JTidy library:
B4X:
Sub Process_Globals
Dim sax As SaxParser
Type ParsedItem(Link As String, Country As String)
Dim items As List
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim t As Tidy
t.Initialize
t.Parse(File.OpenInput(File.DirAssets, "index.html"), File.DirInternal, "temp.xml")
sax.Initialize
Dim In As InputStream = File.OpenInput(File.DirInternal, "temp.xml")
items.Initialize
sax.Parse(In, "sax")
In.Close
Log(items)
End Sub
Sub sax_StartElement (Uri As String, Name As String, Attributes As Attributes)
If sax.Parents.IndexOf("table") <> -1 AND Name = "a" Then
Dim pi As ParsedItem
pi.Initialize
pi.Link = Attributes.GetValue2("", "href")
items.Add(pi)
End If
End Sub
Sub sax_EndElement (Uri As String, Name As String, Text As StringBuilder)
If sax.Parents.IndexOf("table") <> -1 AND Name = "a" Then
Dim pi As ParsedItem = items.Get(items.Size - 1) 'get the last item
pi.Country = text
End If
End Sub
First we create a temporary XML file from the HTML and then we parse it.
I have sax event where flights list is populated with flight details
B4X:
Sub saxFlights_EndElement (Uri As String, Name As String, Text As StringBuilder)
If saxFlights.Parents.IndexOf("tr") > -1 AND Name="td" Then
Select Case c
Case 0: flight.Grad=Text
Case 2: flight.BrojLeta=Text
Case 4: flight.Kompanija=Text
Case 6: flight.TipAviona=Text
Case 8: flight.Vrijeme=Text
Case 10: flight.Status=Text
End Select
c=c+1
If c=12 Then
c = 0
Flights.Add(flight)
flight.Initialize
counter=counter+1
End If
End If
End Sub
In first loop everything is fine and Flights.Add(flight) is added with new flight.
I don't see the code where "c" and "flight" are initialised and assigned but you are probably adding the same object reference again because you have not reDimmed "flight" to be a new instance.
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim c As Int=0: Dim counter As Int=0
Type FlightData(What As String, Value As String)
Type FlightInfo(Grad As String, BrojLeta As String, Kompanija As String, TipAviona As String , Vrijeme As String, Status As String)
Type CityInfo(city As String , Url As String)
Dim city1 As CityInfo: city1.Initialize
Dim flight As FlightInfo: flight.Initialize
Dim Cities, Flights As List
Flights.Initialize: Cities.Initialize
End Sub
I have it initialized here. And yes that's what is happening new "flight" object is not initialized.
Only thing is that I dont understand if I should declare FlightInfo as a Type or a Class ?
You were right agraham
I just needed to reDim flight
B4X:
Dim flight As FlightInfo: flight.Initialize
Now another problem appeared:
Since I am running multiple downloads with HTTPJob
I got Java exception somewhere in HTPJOb module
B4X:
Sub GetInputStream As InputStream
Dim In As InputStream
In = File.OpenInput(HttpUtils2Service.TempFolder, taskId)
Return In
End Sub
B4X:
httpjob_getinputstream (B4A line: 124)
In = File.OpenInput(HttpUtils2Service.TempFolder, taskId)
java.io.FileNotFoundException: /data/data/b4a.example/cache/3: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:416)
at java.io.FileInputStream.<init>(FileInputStream.java:78)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:197)
at b4a.example.httpjob._getinputstream(httpjob.java:209)
at b4a.example.main._jobdone(main.java:607)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.keywords.Common$4.run(Common.java:885)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5191)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:400)
... 17 more
java.io.FileNotFoundException: /data/data/b4a.example/cache/3: open failed: ENOENT (No such file or directory)
I think you will have to keep your own count as I don't think HttpJob keeps its own count of jobs submitted. So you need to count up when you submit the jobs and count down when they complete.