. */ ?> won't work, * it needs to have , just a stupid browser thing * that doesn't make any sense at all. There may very well be * others feel free to add them here, it wont hurt anything besides * page load time a very very slight amount. Removing however can! */ 'a', 'textarea', 'script', 'p', 'style', 'div', 'span' ); function DocumentParser($xml) { //$this->count = 0; $this->xml = $xml; $this->CreateArray($this->xml); $this->nodes = $this->HandleStructArray($this->elements); } public function GetElementsByTagName($tag) { $test = $this->count; $this->searchIndex++; $this->searchNodes[$this->searchIndex] = array(); $this->TraverseSearch('name', $tag); return $this->searchNodes[$this->searchIndex]; } public function GetElementsByID($id) { $test = $this->count; $this->searchIndex++; $this->searchNodes[$this->searchIndex] = array(); $this->TraverseSearch(array('attributes', 'id'), $id); return $this->searchNodes[$this->searchIndex]; } public function GetElementsByAttribute($attr, $attrVal) { $test = $this->count; $this->searchIndex++; $this->searchNodes[$this->searchIndex] = array(); $this->TraverseSearch(array('attributes', $attr), $attrVal); return $this->searchNodes[$this->searchIndex]; } public function &GetNodes() { return $this->nodes; } public function ToString() { $this->xmlString = ''; $this->GenerateXML(); $this->xmlString = '' . trim($this->xmlString); return $this->xmlString; } public function Unlink($killNode, $nodeGroup=array()) { if(empty($nodeGroup)) { $nodeGroup = &$this->nodes; } foreach($nodeGroup as $key=>$val) { if($val == $killNode) { if($val->HasChildren()) { $this->Unlink($killNode, $val); } } } } public function GenerateXML($node=array(), $tabs=0) { $spaces = $tabs * 2; if(empty($node)) { $node = &$this->nodes; } foreach($node as $nodekey=>$nodeval) { if($nodeval->GetTag() == 'text') { $content = trim($nodeval->GetContent()); $content = wordwrap($content, 75 - $spaces); $content = explode("\n", $content); foreach($content as $contentkey=>$contentval) { $this->xmlString .= str_repeat(" ", $spaces) . $contentval . "\n"; } } else if($nodeval->GetTag() == 'kill') { //SKIP! } else if($nodeval->GetTag() == 'placeholder') { if($nodeval->HasChildren()) { $this->GenerateXML($nodeval->GetChildren(), $tabs); } } else if($nodeval->GetTag() == 'ieblock') { $this->xmlString .= str_repeat(" ", $spaces) . "\n"; } else { $line = str_repeat(" ", $spaces) . "<" . $nodeval->GetTag(); if($nodeval->HasAttributes()) { $attributes = $nodeval->GetAttributes(); foreach($attributes as $attrkey=>$attrval) { $line .= " " . $attrkey . "=" . '"' . $attrval . '"'; } } if($nodeval->HasChildren()) { $line .= ">" . "\n"; $this->xmlString .= $line; $tabs++; $this->GenerateXML($nodeval->GetChildren(), $tabs); $tabs--; $this->xmlString .= str_repeat(" ", $spaces) . "GetTag() . "> \n"; } else if(in_array($nodeval->GetTag(), $this->NeedStrictClose)) { $this->xmlString .= $line . ">GetTag() . ">" . "\n"; } else { $this->xmlString .= $line . " />" . "\n"; } } } } //This function converts nodes to node objects private function HandleStructArray($struct, $parentNode='') { foreach($struct as $shallowArray) { $index = count($node); $node[] = new DocumentNode(); $node[$index]->SetTag($shallowArray['name']); $node[$index]->SetIdentifier(microtime()); if($parentNode) { $node[$index]->SetParent($parentNode); } if($shallowArray['content']) { $node[$index]->SetContent($shallowArray['content']); } if(is_array($shallowArray['attributes'])) { $node[$index]->SetAttributes($shallowArray['attributes']); } if($shallowArray['children'] && !empty($shallowArray['children'])) { $node[$index]->AppendChildren($this->HandleStructArray($shallowArray['children'], &$node[$index])); } } return $node; } private function CreateArray($xml) { $xml = str_replace('&', '&', $xml); $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, $xml, $tags); xml_parser_free($parser); $elements = array(); $stack = array(); foreach ($tags as $tag) { $index = count( $elements ); if($tag['type'] == "complete" || $tag['type'] == "open") { $elements[$index] = array(); $elements[$index]['name'] = $tag['tag']; $elements[$index]['attributes'] = empty($tag['attributes']) ? "" : $tag['attributes']; if($tag['type'] == "open") { $stack[count($stack)] = &$elements; if($tag['value']) { $elements[$index]['children'][0]['name'] = "text"; $elements[$index]['children'][0]['content'] = $tag['value']; } $elements = &$elements[$index]['children']; } else if($tag['value']) { $elements[$index]['children'] = array(); $elements[$index]['children'][0]['name'] = "text"; $elements[$index]['children'][0]['content'] = $tag['value']; } } else if($tag['type'] == "cdata") { $elements[$index] = array(); $elements[$index]['name'] = "text"; $elements[$index]['content'] = $tag['value']; } else if($tag['type'] == "close") { $elements = &$stack[count($stack) - 1]; unset($stack[count($stack) - 1]); } else { die("ERROR: " .$tag['type'] . " NOT RECOGNIZED"); } } $this->elements = $elements; } private function TraverseSearch($searchKey, $searchValue, $node=array()) { if(is_array($searchKey)) { $attributes = true; } if(empty($node)) { $node = &$this->nodes; } foreach($node as $nodekey=>$nodeval) { if(is_object($nodeval)) { if($nodeval->HasAttributes() && $attributes) { if($node[$nodekey]->GetAttribute($searchKey[1])) { $this->searchNodes[$this->searchIndex][] = &$node[$nodekey]; } } else if($searchKey == 'name' && $searchValue == $node[$nodekey]->GetTag($searchValue)) { $this->searchNodes[$this->searchIndex][] = &$node[$nodekey]; } if($nodeval->HasChildren()) { $this->TraverseSearch($searchKey, $searchValue, $nodeval->GetChildren()); } } else { //Not an object, lets see what happens } } } } ?>