Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting RSS feed to json is missing pieces

I am trying to convert a RSS feed to json PHP. I get the title, description etc. but I am missing all elements starting with "itunes".

This is for instance not showing in my json:

<itunes:subtitle>Subtitle text here</itunes:subtitle>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But these are showing:

<title>Title here</title>
<description>Description here</description>

I think it has something to do with converting the XML to json, which I use this code for:

function Parse($url) {
    $fileContents= file_get_contents($url);
    $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
    $fileContents = trim(str_replace('"', "'", $fileContents));
    $simpleXml = simplexml_load_string($fileContents);
    $json = json_encode($simpleXml);
    return $json;
}

Can anybody help me understand where the error occurs?

>Solution :

Ahh you are destroying the XML with the string replacement you are doing. Instead all you need to do is convert the tags with : in them to something that will be valid in PHP once converted to JSON

So just do the str_replace() to convert

<itunes:subtitle>Subtitle text here</itunes:subtitle>

To

<itunes_subtitle>Subtitle text here</itunes_subtitle>

That will make those tags legal when converted to a PHP data structure

function Parse($url) {
    $fileContents= file_get_contents($url);
    
    $fileContents = str_replace(':', '_', $fileContents);
    
    $simpleXml = simplexml_load_string($fileContents);
    $json = json_encode($simpleXml);
    return $json;
}

RESULT

stdClass Object
(
    [@attributes] => stdClass Object
        (
            [version] => 2.0
            [xmlns_itunes] => http_//www.itunes.com/dtds/podcast-1.0.dtd
            [xmlns_atom] => http_//www.w3.org/2005/Atom
        )

    [channel] => stdClass Object
        (
            [atom_link] => stdClass Object
                (
                    [@attributes] => stdClass Object
                        (
                            [href] => https_//feeds.soundcloud.com/users/soundcloud_users_30705140/sounds.rss
                            [rel] => self
                            [type] => application/rss+xml
                        )

                )

            [title] => djsksm
            [link] => https_//soundcloud.com/djsksm
            [pubDate] => Tue, 18 Dec 2012 01_53_09 +0000
            [lastBuildDate] => Tue, 18 Dec 2012 01_53_09 +0000
            [ttl] => 60
            [language] => en
            [copyright] => All rights reserved
            [webMaster] => feeds@soundcloud.com (SoundCloud Feeds)
            [description] => Podcast by djsksm
            [itunes_subtitle] => Podcast by djsksm
            [itunes_owner] => stdClass Object
                (
                    [itunes_name] => djsksm
                    [itunes_email] => feeds@soundcloud.com
                )

            [itunes_author] => djsksm
            [itunes_explicit] => no
            [itunes_image] => stdClass Object
                (
                    [@attributes] => stdClass Object
                        (
                            [href] => https_//a1.sndcdn.com/images/default_avatar_original.png
                        )

                )

            [image] => stdClass Object
                (
                    [url] => https_//a1.sndcdn.com/images/default_avatar_original.png
                    [title] => djsksm
                    [link] => https_//soundcloud.com/djsksm
                )

        )

)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading