MiniHtmlParser is a cross platform class that parses html strings and creates a tree with the various elements. It is a less powerful alternative to jTidy or jSoup, however it is simple to use, cross platform and as it is implemented in B4X it can be extended quite easily. Note that many...
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
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">
"what about this text"?
</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))