1

This is how an item from the RSS feed that Im trying to parse looks like:

<item>
<title>
Former cop gets 18 years in prison for shooting civilian to death
</title>
<description>
<![CDATA[
<a href="https://e.vnexpress.net/news/news/former-cop-gets-18-years-in-prison-for-shooting-civilian-to-death-4020110.html"><img width=130 height=100 src="https://i-english.vnecdn.net/2019/11/30/phuoc146331574997301-157508140-8364-4799-1575086150_180x108.jpg" ></a></br>A Dong Nai court sentenced a former traffic policeman to 18 years in prison on Friday for fatally shooting a man in a family feud.
]]>
</description>
<pubDate>Sat, 30 Nov 2019 12:00:00 +0700</pubDate>
<link>
https://e.vnexpress.net/news/news/former-cop-gets-18-years-in-prison-for-shooting-civilian-to-death-4020110.html
</link>
<guid>
https://e.vnexpress.net/news/news/former-cop-gets-18-years-in-prison-for-shooting-civilian-to-death-4020110.html
</guid>
<slash:comments>0</slash:comments>
</item>

This is how I get the title from the RSS feed, I'm using react-native-rss-parser (https://www.npmjs.com/package/react-native-rss-parser):

fetch('https://vnexpress.net/rss/tin-moi-nhat.rss')
     .then((response) => response.text())
     .then((responseData) => rssParser.parse(responseData))
     .then((rss) =>  { 
         this.setState(prevState => ({
             ...prevState,

             title0: rss.items[0] ? rss.items[0].title : '',
             caption0: rss.items[0] ? rss.items[0].description : ''
         }))    
     })
const Feeds = ([
        {
        pic: require('../assets/images/image.jpg'), 
        title: this.state.title0,
        caption: this.state.caption0
      },
])

I'm trying to parse the description and the imageUrl which is nested inside the description tag. How would I do that? Any suggestions?

DarkLord666
  • 35
  • 1
  • 5

1 Answers1

0

using jquery you can parse it and get all the strings inside if you only need the last string you can get the last index at nodeNames list

    str = '<a href="https://e.vnexpress.net/news/news/former-cop-gets-18-years-in-prison-for-shooting-civilian-to-death-4020110.html"><img width=130 height=100 src="https://i-english.vnecdn.net/2019/11/30/phuoc146331574997301-157508140-8364-4799-1575086150_180x108.jpg" ></a></br>A Dong Nai court sentenced a former traffic policeman to 18 years in prison on Friday for fatally shooting a man in a family feud.',
    html = $.parseHTML( str ),
    nodeNames = [];

    // Gather the parsed HTML's node names
    $.each( html, function( i, el ) {
        if(el.nodeName=='#text'){
            nodeNames[ i ] = el.nodeValue;
        }else{
            nodeNames[ i ] = el.innerText;
        }
    });
    console.log(nodeNames);

with only js this will return text content

function parseHTML(html) {
    var t = document.createElement('template');
    t.innerHTML = html;
    return t.content.cloneNode(true).textContent;
}
youssef ali
  • 406
  • 5
  • 11
  • Is there a way to achieve this without using jquery, cause I think I read about this somewhere about how you shouldn't use jquery in React Native (something to do with DOM). – DarkLord666 Dec 03 '19 at 12:16
  • just edited it the parseHtml function should return text, credit :https://stackoverflow.com/a/33321421/11252816 – youssef ali Dec 03 '19 at 12:45