Android Question [Solved] How to pick up HTML content with B4A

Flavio SOuza

Member
Licensed User
Longtime User
Hi, I'm new to B4A, I was programming in PHP about 5 years ago. I'm venturing and enjoying the B4A.

My question is, I have an HTML and I need to get the contents of these HTML. Follow the HTML link and print from the part I need to capture.

I thank you for your attention.

URL -> html: http://appsbr.000webhostapp.com/html.htm
 
Last edited:

Flavio SOuza

Member
Licensed User
Longtime User
That's not what I need... it has to be with Regex.

<td class="txtcentral" width="55%" height="20px" bgcolor="#FEFEFE">&nbsp;Nome: FLAVIO SOUZA SOUZA </td>
<td class="txtcentral" width="45%" bgcolor="#FEFEFE">&nbsp;PIS/PASEP: 123.94502.00-4 </td>

return:
Nome: FLAVIO SOUZA SOUZA
PIS/PASEP: 123.94502.00-4

:|
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Some possibilities:

B4X:
    Dim HTML As String = $"<td class="txtcentral" width="55%" height="20px" bgcolor="#FEFEFE">&nbsp;Nome: FLAVIO SOUZA SOUZA </td>
<td class="txtcentral" width="45%" bgcolor="#FEFEFE">&nbsp;PIS/PASEP: 123.94502.00-4 </td>"$
   
    Log(1)
    For Each Line As String In Regex.Split(CRLF,HTML)
        If Line.Contains("txtcentral") Then
            Log(GetEnclosed(">","<",Line).Replace("&nbsp;",""))
        End If
    Next
   
    Log(2)
   
    'Or any other way you can identify the line
    For Each Line As String In Regex.Split(CRLF,HTML)
        If Line.Contains("Nome:") Or Line.Contains("PIS/PASEP") Then
            Log(GetEnclosed(">","<",Line).Replace("&nbsp;",""))
        End If
    Next
   
    Log(3)
   
    For Each Line As String In Regex.Split(CRLF,HTML)
        If Line.Contains("Nome:") Then Log("Nome:" &  GetEnclosed("Nome:","<",Line).Replace("&nbsp;",""))
        If Line.Contains("PIS/PASEP:") Then Log("PIS/PASEP:" &  GetEnclosed("PIS/PASEP:","<",Line))
    Next
   
    Log(4)
    Dim Test As List = Array As String("Nome:","PIS/PASEP:")
    For Each Line As String In Regex.Split(CRLF,HTML)
        For Each S As String In Test
        If Line.Contains(S) Then Log(S &  GetEnclosed(S,"<",Line))
        Next
    Next

B4X:
Public Sub GetEnclosed(EncStart As String,EncEnd As String, Source As String) As String
    Dim First As Int = Source.IndexOf(EncStart) + EncStart.Length
    If First = -1 Then Return Source
    Dim Second As Int = Source.LastIndexOf(EncEnd)
    If Second = -1 Then Return ""
    If First > Second Then Return ""
    Return Source.SubString2(First,Second)
End Sub
 
Upvote 0

Flavio SOuza

Member
Licensed User
Longtime User
Some possibilities:

B4X:
    Dim HTML As String = $"<td class="txtcentral" width="55%" height="20px" bgcolor="#FEFEFE">&nbsp;Nome: FLAVIO SOUZA SOUZA </td>
<td class="txtcentral" width="45%" bgcolor="#FEFEFE">&nbsp;PIS/PASEP: 123.94502.00-4 </td>"$
  
    Log(1)
    For Each Line As String In Regex.Split(CRLF,HTML)
        If Line.Contains("txtcentral") Then
            Log(GetEnclosed(">","<",Line).Replace("&nbsp;",""))
        End If
    Next
  
    Log(2)
  
    'Or any other way you can identify the line
    For Each Line As String In Regex.Split(CRLF,HTML)
        If Line.Contains("Nome:") Or Line.Contains("PIS/PASEP") Then
            Log(GetEnclosed(">","<",Line).Replace("&nbsp;",""))
        End If
    Next
  
    Log(3)
  
    For Each Line As String In Regex.Split(CRLF,HTML)
        If Line.Contains("Nome:") Then Log("Nome:" &  GetEnclosed("Nome:","<",Line).Replace("&nbsp;",""))
        If Line.Contains("PIS/PASEP:") Then Log("PIS/PASEP:" &  GetEnclosed("PIS/PASEP:","<",Line))
    Next
  
    Log(4)
    Dim Test As List = Array As String("Nome:","PIS/PASEP:")
    For Each Line As String In Regex.Split(CRLF,HTML)
        For Each S As String In Test
        If Line.Contains(S) Then Log(S &  GetEnclosed(S,"<",Line))
        Next
    Next

B4X:
Public Sub GetEnclosed(EncStart As String,EncEnd As String, Source As String) As String
    Dim First As Int = Source.IndexOf(EncStart) + EncStart.Length
    If First = -1 Then Return Source
    Dim Second As Int = Source.LastIndexOf(EncEnd)
    If Second = -1 Then Return ""
    If First > Second Then Return ""
    Return Source.SubString2(First,Second)
End Sub

Omg! Excellent. That's exactly what I needed.

Thank you very much
 
Upvote 0
Top