Android Question Parsing Span element

Almora

Active Member
Licensed User
Longtime User
B4X:
    Dim st As String = $"

                          <div class="col-9 ml-auto text-right p-0">
                                  <span id="allPopulation" class=" allPopulation number-lg">32.519</span>
                            </div>
 
                       "$
    Dim parser As MiniHtmlParser
    parser.Initialize
    Dim root As HtmlNode = parser.Parse(st)
    Dim listgroup As HtmlNode = parser.FindNode(root, "div", parser.CreateHtmlAttribute("class", "col-9 ml-auto text-right p-0"))
    If listgroup.IsInitialized Then
        Dim span1 As HtmlNode = parser.FindNode(listgroup, "span", Null)
        
        Log(parser.GetAttributeValue(span1, "class", ""))
 
    End If

Hello. How can I get this value? >>> 32.519
I get this in the code above. >>> allPopulation number-lg

thanks..


32.519
 

William Lancee

Well-Known Member
Licensed User
Longtime User
B4X:
Dim st As String = $"

                          <div class="col-9 ml-auto text-right p-0">
                                  <span id="allPopulation" class=" allPopulation number-lg">32.519</span>
                            </div>
 
                       "$
    Dim parser As MiniHtmlParser
    parser.Initialize
    Dim RootN As HtmlNode = parser.Parse(st)
    Dim listgroup As HtmlNode = parser.FindNode(RootN, "div", parser.CreateHtmlAttribute("class", "col-9 ml-auto text-right p-0"))
    If listgroup.IsInitialized Then
        Dim span1 As HtmlNode = parser.FindNode(listgroup, "span", Null)
        Log(parser.GetTextFromNode(span1, 0))                "<<<<< this is it
        Log(parser.GetAttributeValue(span1, "class", ""))
 
    End If
 
Upvote 1

TILogistic

Expert
Licensed User
Longtime User
or
B4X:
    Dim text As String = $"
                          <div class="col-9 ml-auto text-right p-0">
                                  <span id="allPopulation" class=" allPopulation number-lg">32.519</span>
                            </div>
 
                       "$
    Log(ReadValueSpan(text))
B4X:
Public Sub ReadValueSpan(Html As String) As String
    Dim Result As String, Pattern As String = $"<\s*span[^>]*>(.*?)<\s*\/\s*span>"$
    Dim Matcher As Matcher = Regex.Matcher(Pattern, Html)
    If Matcher.Find Then Result = Matcher.Group(1)
    Return Result
End Sub
1712937078017.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
I have seen that you have asked these questions before and since I am currently making an app that uses Regex a lot, I leave you the solution to your questions.


B4X:
    Dim text As String = $"
                          <div class="col-9 ml-auto text-right p-0">
                                  <span id="allPopulation" class=" allPopulation number-lg">32.519</span>
                            </div>
 
                       "$
   
    For Each v As Object In ReadValueSpan(text)
        Log(v)
    Next
                     
    Dim text As String = $"
                       <div class="list-group">
                       <input type="checkbox" dataid="101" />
                       <a href="techno1">2023</a>
                       <span> 615 <a href="techno"><i class="fa fa-chevron-circle-right"></i></a></span>
                       </div>
                                         
                       <div class="list-group">
                       <input type="checkbox" dataid="102" />
                       <a href="techno2">2023</a>
                       <span> 715 <a href="techno"><i class="fa fa-chevron-circle-right"></i></a></span>
                       </div>
                       "$
   

    For Each v As Object In ReadValueSpan(text)
        Log(v)
    Next

B4X:
Public Sub ReadValueSpan(Html As String) As List
    Dim Pattern As String = $"<\s*span[^>]*>(.*?)<.*?\/span>"$
    Dim Matcher As Matcher = Regex.Matcher(Pattern, Html)
    Dim Result As List : Result.Initialize
    Do While Matcher.Find
        Result.Add(Matcher.Group(1))
    Loop
    Return Result
End Sub
1712943198835.png


test:
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
@TILogistic

Just curious - what is wrong with my solution in #2?
Sorry, your solution is fine.

I just saw that it may be a more direct solution than MiniHtmlParser, which also uses regular expressions. and that this question had already been asked in another post.

To provide a solution to both posts, I wrote that routine, which can guide you in using regular expressions in other solutions along with MiniHtmlParser.

😔😔🤔🤔🤔🤔
 
Upvote 0
Top