Jump to content

Using PHP to re-structure an XML file for RSS syndication?


matticus

Recommended Posts

Hello,
I have a bit of a problem that is beyond my PHP skills (also, I can't find the newbie forum mentioned in the guidelines, so apologies if this is the wrong place!).  I have an XML file which is generated for me (I have no control over the content or formatting of this file) and I want to reformat it into an RSS-compatible structure.

The data XML I get is grouped by tag, rather than sequenced, like so:
[code]<field name='EVENTIME'>

<time>4:10 p.m.</time>
<time>12:10 p.m.</time>
<time>12 - 1pm</time>
<time>2 - 3pm</time>
<time>6:00 p.m.</time>
<time>10:00 a.m.-Noon</time>
<time>3:10 p.m.</time>
<time>3:30 PM</time>[/code] etc...

Where each tag in the list corresponds to the other tags (the 5th 'time' tag covers the same event as the 5th 'date' tag and 'title' tag, and so on).  I'm trying to rewrite the file so that it looks like an RSS item instead of time time time time title title title title, and so on, like the current XML file.  This is the desired output:

[code]<title>Foo</title>
<time>5 pm</time>
<location>Earth</location>
<description>asdf</description>[/code]

Now I'm good enough with PHP so that I could write the PHP file to export from the database if I could control that step, and bypass this whole parsing/rewriting thing.  Unfortunately this is the XML file and I have no access to the original PHP that generated it (there are lots of tutorials and tips on how to do that).  Can anyone point me to a tutorial to rewrite a file, or failing that, show me how to do what I want?  It seems like it should be relatively simply, but I just don't know where to start.  Thanks!
Here's a class I wrote to parse XML directly to an array:
[code]
<?php
/**
  @author: keeb
  @usage: $xml = new xml("url or filename");
                  populates url or filename in to $xml->data;
  */
class xml {

        private $url;
        private $data;
        private $vals;
        private $debug = false;

        public function __construct($url) {
                $this->url  = $url;
                $xml = file_get_contents($url);
                $data = $this->xml2array($xml);
                $this->data = $data;
                if ($this->debug) {
                        print "<pre>";
                        print_r($data);
                }
        }
        private function &last(&$array){
                if (!count($array))
                        return null;
                end($array);
                return $array[key($array)];
        }

        private function parsexml(&$vals, &$dom, &$lev){
                do {
                        $curr = current($vals);
                        $lev = $curr['level'];
                        $tag = strtolower($curr['tag']);
                        switch ($curr['type'])
                        {
                                case 'open':
                                        if (!isset($dom[$tag]))
                                                $dom[$tag] = array();
                                        array_push($dom[$tag], array());
                                        $new =& $this->last($dom[$tag]);
                                        next($vals);
                                        $this->parsexml(&$vals, $new, $lev);
                                        break;

                        case 'cdata':
                                break;

                        case 'complete':
                                if (!isset($dom[$tag]))
                                        $dom[$tag] = $curr['value'];
                                else
                                        array_push($dom[$tag] , $curr['value']);
                                break;

                        case 'close':
                                return;
                        }
                }
                while (next($vals)!==FALSE);
        }

        private function xml2array($xml){
                $xml_parser = xml_parser_create();
                xml_parse_into_struct($xml_parser, $xml, $vals);
                xml_parser_free($xml_parser);
                reset($vals);
                $dom = array();
                $lev = 0;
                $this->parsexml($vals, $dom, $lev);
                return $dom;
        }

}



?>
[/code]

Good luck, let me know if you need any assistance.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.