views:

26

answers:

0

Hi,

I'm writing an app currently that uses the ExtJS framework. I'm pulling an XML feed from an external site using cURL, loading as simplexml, json_encode-ing it, and writing it to a file a .JSON file which I will later access with ExtJS data store.

However, the formatting is becoming funky because the XML generated from the PHP file uses the

<DAY attr="2010-10-18">
<time attr="09:00 am">
        <show name="Live with Regis and Kelly">
            <sid>4263</sid>
            <network>Syndicated</network>
            <title>David Duchovny, Nicole &quot;Snooki&quot; Polizzi, Jenni &quot;JWoww&quot; Farley</title>
            <ep>29x41</ep>
            <link>http://www.tvrage.com/shows/id-4263/episodes/1064984101&lt;/link&gt;
        </show>
</time>

Which converts to:

{
        "@attributes":
        {
            "attr":"2010-10-18"
        },
        "time":
        [
            {
                "@attributes":
                {
                    "attr":"09:00 am"
                },
                "show":
                {
                    "@attributes":
                    {
                        "name":"Live with Regis and Kelly"
                    },
                    "sid":"4263",
                    "network":"Syndicated",
                    "title":"David Duchovny,
                     Nicole \"Snooki\" Polizzi,
                     Jenni \"JWoww\" Farley",
                    "ep":"29x41",
                    "link":"http:\/\/www.tvrage.com\/shows\/id-4263\/episodes\/1064984101"
                }
            }

Instead of something like:

<time>  
  <timeslot>9:00 am</timeslot>
  <episodes>
    <name>Live With Regis & Kelly</name>
    ...etc

Which would convert to something along the lines of:

DAY:
date: 'dateval',
[
{
    time:
    timeslot: 8pm,
    [
        {
            episode: 
            [
                {
                    name: Live With Regis,
                    sid: 88888,
                    network: ABC,
                    Title:  blahblah,
                    ep: 29x41,
                    link: http://wwot.com/
                },
                {
                    name: Live With Regis2,
                    sid: 88888,
                    network: ABC,
                    Title:  blahblah,
                    ep: 29x41,
                    link: http://wwot.com/
                }
            ]
        },
    ]
},
time:
timeslot: 9pm,    

I'm well aware the syntax isn't completely correct, this is basically pseudo code that I just threw up so you could get an idea of what I'm trying to get at, let me know if you need clarification. You can assume I know how to create valid JSON/XML syntax.

So when the XML converts it to JSON I get a ton of array attributes that aren't actually arrays. What I'm trying to ask is, is there a better way to grab this XML and have it converted to JSON? I'm open to other languages besides PHP for this as well (Python, Perl, etc), my primary end goal is to get the XML from the PHP feed, convert to JSON and write to a file as simply as possible.

Here is my PHP code, you can check out how the feed appears on the cURL link as well.

//Grab XML feed from tvrage and convert to JSON using cURL
//This script will be run once daily to cut down on server load
header('Content-type: text/javascript');
$url = 'http://services.tvrage.com/feeds/fullschedule.php?country=US';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$data = simplexml_load_string($output);
$data = json_encode($data);


//Print to JSON file new data for the week
if ($data) {
    $writeFile = "WeeklySchedule.JSON";
    $fh = fopen($writeFile, 'w') or die("can't open file");
    fwrite($fh,$data);
    fclose($fh);
}