Android Question Read HTML between elements (text)

Hi everyone :rolleyes:
I used Google translate....
I need a library for read html.
Html code:
<p id="text">
Test text
</p>

I need a code read html top and save "Text text" for string b4a
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0

Midimaster

Active Member
Licensed User
You can do it "old school" style. You would only need three code lines to isolate the content. This only works if the ID is always the same:
B4X:
Sub Button1_Click
    Dim locString  As String
    locString=$"<p id="text">Test text</p>"$
    FindContent(locString)
    locString=$"<p id="wrong">Blabla</p> <p id="text">
    Two lines text
    with return</p>"$
    FindContent(locString)
End Sub

Sub FindContent(HtmlString As String) As String
    Dim StartsAt As Int = HtmlString.IndexOf2($"<p id="text">"$,0)+13
    Dim EndsAt As Int=HtmlString.IndexOf2("</p",StartsAt+1)'-1
    Dim Result As String = HtmlString.SubString2(StartsAt,EndsAt)

        Log(" ")
        Log("Scan=" & HtmlString)
        Log("...processing")
        Log("StartAt=" & StartsAt & "   EndsAt=" & EndsAt)
        Log ("Content=" & Result)
        Log("======================")
        Log(" ")
    
    Return Result
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The only reason to make such fragile implementation is if you don't have a good alternative.

Correct code:
B4X:
Dim html As String = $"
<body>
<p 
    id    =
    "text">
&quot;what about this text&quot;? 
</p>
</body>"$
Dim parser As MiniHtmlParser
parser.Initialize
Dim root As HtmlNode = parser.Parse(html)
Dim p As HtmlNode = parser.FindNode(root, "p", parser.CreateHtmlAttribute("id", "text"))
Log(parser.GetTextFromNode(p, 0))
 
Upvote 0
Top