Android Question image from the description tag of rss feed

Almora

Well-Known Member
Licensed User
Longtime User
How can i get image from the description tag of rss feed..
thanks..


B4X:
    Dim xm As Xml2Map

    xm.Initialize

    Dim ParsedData = xm.Parse(rssString) As Map

    Dim rss As Map = ParsedData.Get("rss")

    Dim channel As Map = rss.Get("channel")

    Dim items As List = channel.Get("item")

    For Each item As Map In items

        Dim title As String = item.Get("title")   

        Dim description As String = item.Get("description")


<item>
<title><![CDATA[#The team was dispatched.]]></title>
<description><![CDATA[#CAUTION <br> The team has been dispatched. on duty. <br><img style src="http://graphics8.nytimes.com/images...ly-report/bits-daily-report-thumbStandard.jpg" referrerpolicy="no-referrer">]]></description>
</item>
 

TILogistic

Expert
Licensed User
Longtime User
How can i get image from the description tag of rss feed..
thanks..


B4X:
    Dim xm As Xml2Map

    xm.Initialize

    Dim ParsedData = xm.Parse(rssString) As Map

    Dim rss As Map = ParsedData.Get("rss")

    Dim channel As Map = rss.Get("channel")

    Dim items As List = channel.Get("item")

    For Each item As Map In items

        Dim title As String = item.Get("title")  

        Dim description As String = item.Get("description")
see:
B4X:
        Dim description As String = colitem.Get("description")
        Log(description)
        Dim Matcher2 As Matcher = Regex.Matcher("(http[^\s]+(jpg|jpeg|png|tiff)\b)", description) 'Get Url Image
        If Matcher2.Find Then Log(Matcher2.Match) 'Get Url Image
        Log(Regex.Replace("<(.|\n)*?>", description, "")) 'Delete Tag Html and Get descripcion

Result:
1636848428799.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Note:
Convert XML to JSON
sample post @ilan
B4X:
Public Sub TestAPIGetRSS
'    Dim BaseURL As String = "https://ims.gov.il/sites/default/files/ims_data/rss/alert/rssAlert_general_country_he.xml"
    Dim BaseURL As String = "http://www.ynet.co.il/Integration/StoryRss2.xml"
      
    Wait For (APIGetRSS(BaseURL)) Complete (Result As String)
    If Result.Length = 0 Then Return
  
    Dim XML As Xml2Map
    XML.Initialize
    Dim ParsedData As Map = XML.Parse(Result)
    TextArea1.Text = ParsedData.As(JSON).ToString
  
    Dim parser As JSONParser
    parser.Initialize(TextArea1.Text)
    Dim mRoot As Map = parser.NextObject
    Dim rss As Map = mRoot.Get("rss")

'    Dim Attributes As Map = rss.Get("Attributes")
'    Dim version As String = Attributes.Get("version")
  
    Dim channel As Map = rss.Get("channel")
  
'    Dim image As Map = channel.Get("image")
'    Dim link As String = image.Get("link")
'    Dim title As String = image.Get("title")
'    Dim url As String = image.Get("url")
'    Dim copyright As String = channel.Get("copyright")
'    Dim lastBuildDate As String = channel.Get("lastBuildDate")
'    Dim link As String = channel.Get("link")
'    Dim description As String = channel.Get("description")
'    Dim language As String = channel.Get("language")
'    Dim title As String = channel.Get("title")
'    Dim pubDate As String = channel.Get("pubDate")

    Dim item As List = channel.Get("item")
    For Each colitem As Map In item
'        Dim link As String = colitem.Get("link")
'        Log(link)
        Dim description As String = colitem.Get("description")
        Log(description)
        Dim Matcher2 As Matcher = Regex.Matcher("(http[^\s]+(jpg|jpeg|png|tiff)\b)", description) 'Get Url Image
        If Matcher2.Find Then Log(Matcher2.Match) 'Get Url Image
        Log(Regex.Replace("<(.|\n)*?>", description, "")) 'Delete Tag Html and Get descripcion
'        Dim guid As String = colitem.Get("guid")
'        Log(guid)
'        Dim title As String = colitem.Get("title")
'        Log(title)
'        Dim pubDate As String = colitem.Get("pubDate")
'        Log(pubDate)
'        Dim tags As String = colitem.Get("tags")
'        Log(tags)
        Log("--------------------" & CRLF)
      
    Next
End Sub
Public Sub APIGetRSS(URL As String) As ResumableSub
    Dim ResultURL As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download(URL)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            ResultURL = j.GetString
        End If
    Catch
        Log(LastException.Message)
    End Try
    j.Release
    Return ResultURL
End Sub

1636848739155.png
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I would have used MiniHtmlParser for this:
B4X:
Dim s As String = $"#CAUTION <br> The team has been dispatched. on duty. <br><img style src="http://graphics8.nytimes.com/images/2011/11/30/technology/bits-daily-report/bits-daily-report-thumbStandard.jpg" referrerpolicy="no-referrer""$
Dim parser As MiniHtmlParser
parser.Initialize
Dim rootnode As HtmlNode = parser.Parse(s)
Dim img As HtmlNode = parser.FindNode(rootnode, "img", Null)
If img.IsInitialized Then
    Dim src As String = parser.GetAttributeValue(img, "src", "")
    Log(src)
End If

Once you get the link, you can easily show the image with SimpleMediaManager.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
or
B4X:
    Dim XML As Xml2Map
    XML.Initialize
    Dim Text As String = $"<description><![CDATA[#CAUTION <br> The team has been dispatched. on duty. <br><img style src="http://graphics8.nytimes.com/images...ly-report/bits-daily-report-thumbStandard.jpg" referrerpolicy="no-referrer">]]></description>"$
    Dim data As Map = XML.Parse(Text)
    Dim Matcher2 As Matcher = Regex.Matcher("(http[^\s]+(jpg|jpeg|png|tiff)\b)", data.Get("description"))
    If Matcher2.Find Then Log(Matcher2.Match) 'Get Url Image
    Log(Regex.Replace("<(.|\n)*?>",data.Get("description"),"").Replace("#CAUTION","").Trim) 'Get descripcion
1636896308003.png
 
Upvote 0
Top