tagName=$tagname; $this->value=$value; if(!($tagname===FALSE)){ $this->properties=array(); $this->childNodes=array(); $this->parentNode=FALSE; } } function __destruct() { unset ($this->properties); unset ($this->childNodes); } function appendChild($element){ array_push($this->childNodes,$element); $element->parentNode=$this; return $element; } function setAttribute($attribute,$value){ $this->properties[$attribute]=$value; } function getAttribute($attribute){ if(isset($this->properties[$attribute])) return($this->properties[$attribute]); else return ""; } function removeChild($element){ for($i=0;$ichildNodes);$i++){ if($this->childNodes[$i]==$element){ $child=$this->childNodes[$i]; array_splice ( $this->childNodes, $i,1); return $child; } } $element->parentNode=FALSE; return $element; } function getElementsByTagName($tagName,$arr=FALSE){ if($arr===FALSE) $arr=array(); if($tagName==$this->tagName) array_push($arr,$this); for($i=0;$ichildNodes);$i++){ $arr=$this->childNodes[$i]->getElementsByTagName($tagName,$arr); } return $arr; } function getElementsByName($name,$arr=FALSE){ if($arr===FALSE) $arr=array(); if($name==$this->getAttribute("name")) array_push($arr,$this); for($i=0;$ichildNodes);$i++){ $arr=$this->childNodes[$i]->getElementsByName($name,$arr); } return $arr; } function getElementById($id){ if(isset($this->properties["id"]) && $this->properties["id"]==$id) return($this); for($i=0;$ichildNodes);$i++){ $match=$this->childNodes[$i]->getElementById($id); if($match) return $match; } return FALSE; } function toXML(){ if($this->tagName===FALSE) return $this->value; $elem="<".$this->tagName; foreach($this->properties as $attrib => $value) { $elem=$elem." ".$attrib."=\"".$value."\""; } if(count($this->childNodes)>0){ $elem=$elem.">"; for($i=0;$ichildNodes);$i++){ $elem=$elem . $this->childNodes[$i]->toXML(); } $elem=$elem."tagName.">"; }else{ $elem=$elem." />"; } return $elem; } function toFile($fileName){ $fileWriter=new writableFile($fileName); $fileWriter->write($this->toXML()); } } class XMLDocument extends XMLElement{ public $head; public $body; function __construct($titleText){ parent::__construct("html"); $this->head=$this->appendChild(new XMLElement("head")); $title=$this->head->appendChild(new XMLElement("title")); $title->appendChild(new XMLElement(FALSE,$titleText)); $this->body=$this->appendChild(new XMLElement("body")); } function __destruct() { parent::__destruct(); } } class XMLParser { public $topElement; private $currentElement; private $xmlString=""; private $xmlStringLength=0; private $offset=0; private $level=0; function __construct($xmlString){ $this->xmlString=$xmlString; $this->xmlStringLength=strlen($xmlString); $this->topElement=new XMLElement("document"); $this->currentElement=$this->topElement; $this->offset=$this->findNextTagOffset(); if($this->offset==(-1)) return; $this->getNextElement(); } function getTree(){ return $this->topElement->childNodes[0]; } private function getNextElement(){ $nextOffset=$this->findNextTagOffset(); $nextRelativeOffset=$nextOffset-$this->offset; if($nextOffset==(-1)) return; if($nextRelativeOffset>0){ $this->currentElement->appendChild(new XMLElement(FALSE,substr($this->xmlString,$this->offset,$nextRelativeOffset))); $this->offset=$nextOffset; $this->getNextElement(); }else if($nextRelativeOffset==0){ $tagTypeRegExp='|<(/?)[^>]*?(/?)>()|siA'; $tagTypeRegExpResult=array(); preg_match ( $tagTypeRegExp, $this->xmlString, $tagTypeRegExpResult, PREG_OFFSET_CAPTURE, $this->offset ); $tagType="open"; if($tagTypeRegExpResult[1][0]=="/") $tagType="close"; if($tagTypeRegExpResult[2][0]=="/") $tagType="single"; switch($tagType){ case "open": $this->currentElement=$this->currentElement->appendChild($this->parseElement($tagTypeRegExpResult[0][0])); break; case "close": $this->currentElement=$this->currentElement->parentNode; break; case "single": $this->currentElement->appendChild($this->parseElement($tagTypeRegExpResult[0][0])); break; } $this->offset=$tagTypeRegExpResult[3][1]; $this->getNextElement(); }else{ $this->offset=$nextOffset; } return; } private function findNextTagOffset(){ $findNextTagRegExp='|xmlString, $findNextTagRegExpResult, PREG_OFFSET_CAPTURE, $this->offset ); if($match==0) return -1; else return $findNextTagRegExpResult[0][1]; } private function parseElement($xmlElementString){ $xmlAttribRegExp='|([a-z0-9]+)\s*=\s*([\'"])(.*?)\2|si'; $xmlTagNameRegExp='|<([a-z0-9]+)|siA'; $xmlAttribRegExpResult=array(); $xmlTagNameRegExpResult=array(); preg_match_all ( $xmlAttribRegExp, $xmlElementString, $xmlAttribRegExpResult, PREG_SET_ORDER); preg_match ( $xmlTagNameRegExp, $xmlElementString, $xmlTagNameRegExpResult ); $result=new XMLElement($xmlTagNameRegExpResult[1]); foreach($xmlAttribRegExpResult as $k => $v){ $result->setAttribute($v[1], $v[3]); } return $result; } } ?>