首页 > 开发 > PHP > 正文

使用PHP 5.0 轻松解析XML文档(2)

2024-05-04 22:53:56
字体:
来源:转载
供稿:网友
文件:simpledocumentparser.php

<?php/** *========================================================= * * @author     hahawen(大龄青年)   * @since      2004-12-04 * @copyright  copyright (c) 2004, nxcoder group * *========================================================= *//** * class simpledocumentparser * use sax parse xml file, and build simpledocumentobject * all this pachage's is work for xml file, and method is action as dom. * * @package smartweb.common.xml * @version 1.0 */class simpledocumentparser{ private $domrootobject = null; private $currentno = null; private $currentname = null; private $currentvalue = null; private $currentattribute = null; public function getsimpledocument() {     return $this->domrootobject; } public function parse($file) {        $xmlparser = xml_parser_create();     xml_parser_set_option($xmlparser,xml_option_case_folding, 0);     xml_parser_set_option($xmlparser,xml_option_skip_white, 1);     xml_parser_set_option($xmlparser, xml_option_target_encoding, 'utf-8');     xml_set_object($xmlparser, $this);     xml_set_element_handler($xmlparser, "startelement", "endelement");     xml_set_character_data_handler($xmlparser, "characterdata");        if (!xml_parse($xmlparser, file_get_contents($file)))            die(sprintf("xml error: %s at line %d", xml_error_string(xml_get_error_code($xmlparser)),
        xml_get_current_line_number($xmlparser))); xml_parser_free($xmlparser); } private function startelement($parser, $name, $attrs) { $this->currentname = $name; $this->currentattribute = $attrs; if($this->currentno == null) { $this->domrootobject = new simpledocumentroot($name); $this->currentno = $this->domrootobject; } else { $this->currentno = $this->currentno->createnode($name, $attrs); } } private function endelement($parser, $name) { if($this->currentname==$name) { $tag = $this->currentno->getseq(); $this->currentno = $this->currentno->getpnodeobject(); if($this->currentattribute!=null && sizeof($this->currentattribute)>0) $this->currentno->setvalue($name, array('value'=>$this->currentvalue,
       'attrs'=>$this->currentattribute)); else $this->currentno->setvalue($name, $this->currentvalue); $this->currentno->removenode($tag); } else { $this->currentno = (is_a($this->currentno, 'simpledocumentroot'))? null:
        $this->currentno->getpnodeobject(); } } private function characterdata($parser, $data) { $this->currentvalue = iconv('utf-8', 'gb2312', $data); } function __destruct() { unset($this->domrootobject); }}?>

  文件:simpledocumentbase.php

<?php/** *========================================================= * * @author     hahawen(大龄青年)   * @since      2004-12-04 * @copyright  copyright (c) 2004, nxcoder group * *========================================================= *//** * abstract class simpledocumentbase * base class for xml file parse * all this pachage's is work for xml file, and method is action as dom. * * 1/ add/update/remove data of xml file. * 2/ explode data to array. * 3/ rebuild xml file * * @package smartweb.common.xml * @abstract * @version 1.0 */abstract class simpledocumentbase{ private $nodetag = null; private $attributes = array(); private $values = array(); private $nodes = array();    function __construct($nodetag)    {        $this->nodetag = $nodetag;    }    public function getnodetag()    {     return $this->nodetag;    }    public function setvalues($values){     $this->values = $values;    }    public function setvalue($name, $value)    {     $this->values[$name] = $value;    }    public function getvalue($name=null)    {     return $name==null? $this->values: $this->values[$name];    }    public function removevalue($name)    {     unset($this->values["$name"]);    }    public function setattributes($attributes){        $this->attributes = $attributes;    }    public function setattribute($name, $value)    {     $this->attributes[$name] = $value;    }    public function getattribute($name=null)    {        return $name==null? $this->attributes: $this->attributes[$name];    }    public function removeattribute($name)    {     unset($this->attributes["$name"]);    }    public function getnodessize()    {        return sizeof($this->nodes);    }    protected function setnode($name, $nodeid)    {     $this->nodes[$name] = $nodeid;    }    public abstract function createnode($name, $attributes);    public abstract function removenode($name);    public abstract function getnode($name=null);    protected function getnodeid($name=null)    {     return $name==null? $this->nodes: $this->nodes[$name];    }    protected function createnodebyname($rootnodeobj, $name, $attributes, $pid)    {        $tmpobject = $rootnodeobj->createnodeobject($pid, $name, $attributes);        $key = isset($attributes[id])? $name.'_'.$attributes[id]: $name.'_'.$this->getnodessize();        $this->setnode($key, $tmpobject->getseq());     return $tmpobject;    }    protected function removenodebyname($rootnodeobj, $name)    {        $rootnodeobj->removenodebyid($this->getnodeid($name));        if(sizeof($this->nodes)==1)            $this->nodes = array();     else      unset($this->nodes[$name]);    }    protected function getnodebyname($rootnodeobj, $name=null)    {     if($name==null)     {            $tmplist = array();            $tmpids = $this->getnodeid();            foreach($tmpids as $key=>$id)             $tmplist[$key] = $rootnodeobj->getnodebyid($id);            return $tmplist;     }     else     {      $id = $this->getnodeid($name);      if($id===null)      {             $tmpids = $this->getnodeid();             foreach($tmpids as $tkey=>$tid)             {              if(strpos($key, $name)==0)              {               $id = $tid;               break;              }             }      }      return $rootnodeobj->getnodebyid($id);     }    }    public function findnodebypath($path)    {     $pos = strpos($path, '|');     if($pos<=0)     {      return $this->getnode($path);        }        else        {         $tmpobj = $this->getnode(substr($path, 0, $pos));         return is_object($tmpobj)? $tmpobj->findnodebypath(substr($path, $pos+1)): null;        }    }    public function getsavedata()    {        $data = $this->values;        if(sizeof($this->attributes)>0)         $data[attrs] = $this->attributes;        $nodelist = $this->getnode();        if($nodelist==null)         return $data;        foreach($nodelist as $key=>$node)        {         $data[$key] = $node->getsavedata();        }     return $data;    }    public function getsavexml($level=0)    {     $prefixspace = str_pad("", $level, "/t");     $str = "$prefixspace<$this->nodetag";        foreach($this->attributes as $key=>$value)         $str .= " $key=/"$value/"";        $str .= ">/r/n";        foreach($this->values as $key=>$value){            if(is_array($value))            {                $str .= "$prefixspace/t<$key";                foreach($value[attrs] as $attkey=>$attvalue)                 $str .= " $attkey=/"$attvalue/"";                $tmpstr = $value[value];            }            else            {             $str .= "$prefixspace/t<$key";             $tmpstr = $value;            }            $tmpstr = trim(trim($tmpstr, "/r/n"));            $str .= ($tmpstr===null || $tmpstr==="")? " />/r/n": ">$tmpstr</$key>/r/n";        }        foreach($this->getnode() as $node)         $str .= $node->getsavexml($level+1)."/r/n";     $str .= "$prefixspace</$this->nodetag>";     return $str;    }    function __destruct()    {     unset($this->nodes, $this->attributes, $this->values);    }}?>      


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表