【T11】array2XML

class array2XML{
   private $XMLDom;
   private $rootNode;
   private $rootName;
   private $array;
   private $eregPatternList;


  function __construct($rootName,$array,$patterns=array( "List$")){
    $ this->eregPatternList=$patterns;
    $ this->array=$array;
    $ this->rootName=$rootName;
    $ this->XMLDom= new DOMDocument();
    $ this->rootNode=$ this->XMLDom->createElement($rootName);
  }

   public function getXMLDom(){
    $ this->action($ this->array,$ this->rootNode);
    $ this->XMLDom->appendChild($ this->rootNode);
    $ this->XMLDom->saveXML();
     return $ this->XMLDom;
  }

   public function action($arr,$rootNode){
    $rootName=$rootNode->nodeName;
     foreach ($arr as $k=>$v){
     if (is_numeric($k)){
      $match=array();
       foreach ($ this->eregPatternList as $pattern){
         if (ereg($pattern,$rootName,$match)){
           break;
        }  
      }
       if (empty($match[0])){
        $str=substr($ this->rootName,0,5);
      }
       else {
        $str=str_replace($match[0],"",$rootName);
      }
    } else {
      $str=$k;
    }
    
     if (is_array($v)){ //如果节点为数组
      $childNode=$ this->XMLDom->createElement($str);
      $ this->action($v,$childNode);
      $rootNode->appendChild($childNode);
    } else {
      $element=$ this->XMLDom->createElement($str);
      $textNode=$ this->XMLDom->createTextNode($v);
      $element->appendChild($textNode);
      $rootNode->appendChild($element);
    }
  }
  }
}
上帝啊,原谅我又不写注释吧。
明天补上。2009-11-8

你可能感兴趣的:(PHP,职场,休闲,array2XML)