Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 27th, 2012  |  syntax: None  |  size: 4.26 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. $str =<<<EOF
  3. <package packagerversion="1.8.1" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
  4.  <name>symfony</name>
  5.  <channel>pear.symfony-project.com</channel>
  6.  <summary>Symfony is a complete framework designed to optimize the development of web applications.</summary>
  7.  <description>Symfony is a complete framework designed to optimize the development of web applications by way of several key features.
  8.    For starters, it separates a web application&apos;s business rules, server logic, and presentation views.
  9.    It contains numerous tools and classes aimed at shortening the development time of a complex web application.
  10.    Additionally, it automates common tasks so that the developer can focus entirely on the specifics of an application.
  11.    The end result of these advantages means there is no need to reinvent the wheel every time a new web application is built!</description>
  12.  <lead>
  13.   <name>Fabien POTENCIER</name>
  14.   <user>fabpot</user>
  15.   <email>fabien.potencier@symfony-project.com</email>
  16.   <active>yes</active>
  17.  </lead>
  18.  <date>2011-09-16</date>
  19.  <time>11:37:29</time>
  20.  <version>
  21.   <release>1.4.14</release>
  22.   <api>1.4.0</api>
  23.  </version>
  24.  <stability>
  25.   <release>stable</release>
  26.   <api>stable</api>
  27.  </stability>
  28. EOF;
  29.  
  30. //xml is a file
  31. //xml is a string
  32. //return a php array
  33. class phpXMLParser{
  34.  
  35.     //default
  36.     const XMLFILE =  true;
  37.     const XMLSTR  =  false;
  38.  
  39.     private static $phparr = array();
  40.  
  41.     private static $phparrTemplate = "";
  42.  
  43.     private static $xmlstring = '';
  44.  
  45.     public function __construct($xml, $type = true)
  46.     {
  47.         self::setXML($xml, $type);
  48.     }
  49.     //set xml file of string
  50.     static public function setXML($xml, $type)
  51.     {
  52.         if (true === $type) {
  53.             if(file_exists($xml))
  54.             {
  55.                 self::$xmlstring = file_get_contents($xml);
  56.             }
  57.         }
  58.         else if(false === $type)
  59.         {
  60.             if (is_string($xml)) {
  61.                 self::$xmlstring = $xml;
  62.             }
  63.         }
  64.     }
  65.  
  66.     static public function setTemplate($template = "")
  67.     {
  68.         self::$phparrTemplate = $template;
  69.     }
  70.     //get xml
  71.     static public function getXML()
  72.     {
  73.         return self::$xmlstring;
  74.     }
  75.  
  76.     //parse xml return an PHP array
  77.     static public function run()
  78.     {
  79.        return self::parseXML(self::getXML());
  80.     }
  81.     private function parseXML($xml)
  82.     {
  83.         // parsexml
  84.         preg_match_all("/<(\w+)>/s", $xml ,$tags);
  85.        
  86.         /*<sss><dd>sss</dd><ff>eee</fff></sss>
  87.         preg_match_all("/<.*?>.*?.(\w+|\w*).*?<\/.*?>/s", $xml, $contents);
  88.          */
  89.  
  90.         $arr = array();
  91.         foreach (end($tags) as $val) {
  92.             preg_match("/<$val>(.*?)<\/$val>/s", $xml, $arr[]);
  93.         }
  94.  
  95.         foreach ($arr as $key1 => & $val1) {
  96.             $val1 = end($val1);
  97.         }
  98.         self::$phparr = array_combine(end($tags), $arr);
  99.         /*
  100.         // create a PHP file
  101.         if (!file_exists($file)) {
  102.             file_put_contents($file, var_export($phparr, true));
  103.         }
  104.         else {
  105.             self::$phparr = include($file);
  106.         }
  107.          */
  108.          return self::$phparr;
  109.     }
  110. }
  111. $xmlparser = new phpXMLParser($str, false);
  112. var_dump($xmlparser::run());
  113. exit;
  114. //phpXMLParser::run();
  115. function readxml($xml)
  116. {
  117.         $arr = array();
  118.         preg_match_all("/<(\w+)>/", $xml ,$k);
  119.     /*
  120.         preg_match_all("/<.*?>(.*?)<\/.*?>/s", $xml, $contents);
  121.         var_dump($k[1]);
  122.         var_dump($contents);
  123.         exit;
  124.      */
  125.         foreach (end($k) as $key => $val) {
  126.             preg_match("/<$val>(.*?)<\/$val>/s", $xml, $arr[]);
  127.         }
  128.         foreach ($arr as $key1 => & $val1) {
  129.             $val1 = end($val1);
  130.         }
  131.         //$arrkey = preg_match_all("/<\w+>/", $xml ,$k);
  132.         //$arrval = preg_match_all("/>\w*(.*?)<\//s", $xml ,$v);
  133.         //$arrval = preg_match_all("/>(.*?)<\//s", $xml ,$v);
  134.         //$arrkey = preg_split('/[(^<\W+>*<\/\W+>$)]+/', $xml);
  135.         //return $arr;
  136.         $arr = array_combine(end($k), $arr);
  137.         return $arr;
  138. }
  139. function test($arr)
  140. {
  141. }
  142. //var_dump(readxml($str));