示例#1
0
 /**
  * automaticaly convert XML from the specified XML string and return as CSV string
  * @param string $url
  */
 public static function convertString($xmlString)
 {
     $x = new XmlToCsv();
     echo $x->xml($xmlString)->autoConvert();
 }
示例#2
0
 /**
  * This is the function which does the whole magic conversion.
  * All previously set up paramtere are used to extend the default functionality. 
  * 
  * @return mixed 
  *            - string - if $output is set to 'string' and the conversion is OK
  *            - bool - otherwise. True on success and false on failure   
  */
 public function autoConvert($map = null)
 {
     $o = $this->output;
     // save the output setting for later
     $this->output('array');
     // return the parsed XML as an array
     $m = $this->map;
     // save the map setting for later
     $this->map(array());
     // do not map any fields in the XML, just return them as they are
     $csv = parent::autoConvert();
     $this->output($o);
     $this->map($m);
     // return the previous setting
     if ($map !== null) {
         $this->map($map);
     }
     unset($csv[0]);
     $new = array();
     foreach ($csv as $key => $value) {
         if ($this->importFrom > $key or $this->importTo < $key) {
             continue;
         }
         // only process values in the selected range
         foreach ($this->map as $k => $v) {
             if ($k == '##IMAGE##') {
                 continue;
             }
             // this is a special field, it should not go to the output
             if (!is_array($v)) {
                 $fields = explode(';', $v);
             } else {
                 $fields = $v;
             }
             if ($k == 'description') {
                 $new[$key][$k] = '<![CDATA[';
             } else {
                 $new[$key][$k] = '';
             }
             foreach ($fields as $f) {
                 switch ($f) {
                     case '##IMAGE##':
                         $new[$key][$k] .= isset($value[$this->map[$f]]) ? "<img src=\"" . htmlspecialchars($value[$this->map[$f]]) . "\" />" : '';
                         break;
                     default:
                         $new[$key][$k] .= isset($value[trim($f)]) ? $value[trim($f)] : $f;
                         break;
                 }
             }
             if ($k == 'description') {
                 $new[$key][$k] .= ']]>';
             }
             // re-format the date to RFC-822
             if ($k == 'pubDate') {
                 $new[$key][$k] = self::formatDate($new[$key][$k]);
             }
         }
     }
     if ($this->output == 'array') {
         return $new;
     }
     ob_start();
     echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
     echo "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
     echo "  <channel>\n";
     foreach ($this->channel as $key => $value) {
         switch ($key) {
             case "pubDate":
                 echo "      <{$key}>" . self::formatDate($value) . "</{$key}>\n";
                 break;
             case "image":
                 echo "      <image>\n";
                 foreach ($value as $k => $v) {
                     echo "          <{$k}>{$v}</{$k}>\n";
                 }
                 echo "      </image>\n";
                 break;
             default:
                 echo "      <{$key}>{$value}</{$key}>\n";
                 break;
         }
     }
     echo '      <atom:link href="http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '" rel="self" type="application/rss+xml" />' . "\n";
     foreach ($new as $key => $value) {
         echo "      <item>\n";
         foreach ($value as $k => $v) {
             switch ($k) {
                 case "media":
                 case "enclosure":
                     echo "      <{$k} ";
                     $v = trim(preg_replace("/(^[\r\n]*|[\r\n]+)[\\s\t]*[\r\n]+/", "\n", $v));
                     $v = explode(PHP_EOL, $v);
                     echo 'url="' . trim($v[0]) . '" length="' . trim($v[3]) . '" type="' . trim($v[2]) . '" title="' . trim($v[4]) . '"';
                     echo " />\n";
                     break;
                 default:
                     echo "          <{$k}>{$v}</{$k}>\n";
                     break;
             }
         }
         echo "      </item>\n";
     }
     echo "  </channel>\n";
     echo "</rss>";
     if ($this->output == 'file') {
         header("Content-Disposition: attachment; filename=\"" . $this->filename . "\";");
         header("Pragma: public");
         header("Expires: 0");
         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
         header("Cache-Control: private", false);
         header("Content-Transfer-Encoding: binary");
     }
     if ($this->output == 'echo' or $this->output == 'file') {
         header('Content-Type: application/rss+xml; charset=utf-8');
         echo ob_get_clean();
         return true;
     } else {
         return ob_get_clean();
     }
 }