文件: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 pachages 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)), |
文件: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 pachages 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); } } ?> |