matticus Posted December 5, 2006 Share Posted December 5, 2006 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! Link to comment https://forums.phpfreaks.com/topic/29585-using-php-to-re-structure-an-xml-file-for-rss-syndication/ Share on other sites More sharing options...
willfitch Posted December 6, 2006 Share Posted December 6, 2006 Yes, seems like SimpleXML will be your best friend. It is simple, like the name says, but very powerful.http://us3.php.net/simplxmlLet me know if you need more help. Link to comment https://forums.phpfreaks.com/topic/29585-using-php-to-re-structure-an-xml-file-for-rss-syndication/#findComment-135857 Share on other sites More sharing options...
keeB Posted December 6, 2006 Share Posted December 6, 2006 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. Link to comment https://forums.phpfreaks.com/topic/29585-using-php-to-re-structure-an-xml-file-for-rss-syndication/#findComment-135911 Share on other sites More sharing options...
willfitch Posted December 7, 2006 Share Posted December 7, 2006 Stop reinventing the wheel!There are tons of built in XML extensions: DOM, SimpleXML, etc. Link to comment https://forums.phpfreaks.com/topic/29585-using-php-to-re-structure-an-xml-file-for-rss-syndication/#findComment-136600 Share on other sites More sharing options...
matticus Posted December 12, 2006 Author Share Posted December 12, 2006 Thanks! I think SimpleXML is going to do what I need it to...I just have to take time to figure out the right arguments. Link to comment https://forums.phpfreaks.com/topic/29585-using-php-to-re-structure-an-xml-file-for-rss-syndication/#findComment-139381 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.