download xmltree.inc
Language: NonCode
LOC: 0
Project Info
HPE
Server: SourceForge
Type: cvs
...ceForge\h\hpe\hpe\hpe1\HPE\
   .cvsignore
   clickerr.inc
   config.inc
   Config_File.class.php
   database.inc
   database.mysql.inc
   database.pear.inc
   FormChek.js
   HPE.inc
   HPEbase.inc
   HPEUsers.default.xml
   HPEUsers.guest.xml
   layout.js
   local.inc
   modules.inc
   ...andlers.addChannel.ftsc
   navcond.js
   newsaccess.inc
   newssource.inc
   newstools.js
   overlib.js
   overlib_mini.js
   pagemaker.inc
   pages.inc
   PHPClientSniffer.inc
   phpodp.inc
   publisher.inc
   services.inc
   sitelister.inc
   Smarty.addons.php
   Smarty.class.php
   Smarty_Compiler.class.php
   sourcetools.js
   thememaker.inc
   toparser.inc
   user.edit.inc
   user.inc
   xmlrpc.inc
   xmlrpcs.inc
   xmltree.inc

<?

/* Copyright (c) 1999 Eric van der Vlist / Dyomedea

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL ERIC VAN DER VLIST AND/OR DYOMEDEA BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

http://downloads.dyomedea.com/php3/generic/xml/xmltree.inc

*/

if(!defined("HPEXMLTREEnomultiinc")) {
define("HPEXMLTREEnomultiinc", 1);



$XMLTrees= array();
$xmltree_inc = true;
$xmltree_release = "991026";

function XMLTREEstartElement($parser, $name, $attrs)
{
	global $XMLtrees;
	$XMLtrees[$parser]->start_elt($name, $attrs);
}


function XMLTREEendElement($parser, $name)
{
       global $XMLtrees;
        $XMLtrees[$parser]->pop();
}

function XMLString($parser, $text)
{
       global $XMLtrees;
		if ($text=="&") $text="&amp;";
        $XMLtrees[$parser]->addString($text);
}

class XMLtree {

		var $nodes =array();
		var $ids =array();
		var $curpath = "";
		var $fp;
		var $mode;
		var $filename;
		var $name = "";

		function parseString($string) {
			global $XMLtrees;
			$this->nodes = array();
			$this->ids = array();
			$xml_parser = xml_parser_create();
			$XMLtrees[$xml_parser]= $this;
			xml_set_element_handler($xml_parser, "XMLTREEstartElement", "XMLTREEendElement");
			xml_set_character_data_handler($xml_parser, "XMLString");
		    if (!xml_parse($xml_parser, $string, true)) {
		        return sprintf("XML error: %s at line %d",
		                    xml_error_string(xml_get_error_code($xml_parser)),
		                    xml_get_current_line_number($xml_parser));
		    }
			$tree = $XMLtrees[$xml_parser];
			$this->nodes = $XMLtrees[$xml_parser]->nodes;
			@$this->ids = $XMLtrees[$xml_parser]->attributes;
			$this->name = $XMLtrees[$xml_parser]->name;
			unset($XMLtrees[$xml_parser]);
			xml_parser_free($xml_parser);
			return false;

		}

	function parse($filename, $mode="r") {
			
		$this->mode = $mode;
		$this->filename = $filename;
		if ($mode != "r") { $mode = "r+"; }
		if (!($this->fp = fopen($filename, $mode))) {
			die("could not open XML input");
		}
		$data = fread($this->fp, filesize($filename));
		fclose($this->fp);
		return $this->parseString($data);
	}

	function start_elt($name, $attrs) {
		$name = strtolower($name);
		if ($this->name=="") {
			$this->name='/'.$name.'(1)';
		}
		$path = "$this->curpath/$name";
		@$relpath = "$name(".(++ $this->ids[$path]).")";
		$fullpath = "$this->curpath/$relpath";

		$this->nodes[$fullpath]["attributes"] = $attrs;
		$this->nodes[$fullpath]["name"] = $name;
		$this->nodes[$fullpath]["text"] = "";
		$this->nodes[$fullpath]["elements"] = array();

		$this->nodes[$this->curpath]["elements"][] = $name;
		@$this->nodes[$this->curpath]["text"] .= "<\$$relpath>";

		$this->curpath = $fullpath;

       }

	function pop () {
		$this->curpath = substr($this->curpath, 0, strrpos($this->curpath, "/")); 		
	}

	function addString($text) {
		$this->nodes[$this->curpath]["text"] .= trim($text);
	}


	function toStringByPath($path, $self=false, $dep=0) {

		if (!@is_array($this->nodes[$path])) {
			return false;
		}
		$txt = "";
		$tab="\n";
		for ($i=0; $i<$dep; $i++){
			$tab .="  ";
		}
		$inn = "";
		$pieces = explode("<$", $this->nodes[$path]["text"]);
		while ( list( $key, $val ) = each($pieces) ) {
			if (preg_match("|^([^(]*)\\(([^)]*)\\)>(.*)$|m", $val, $matches)) {
				@$name = $matches[1];
				$id = $matches[2];
				$txtpiece = trim($matches[3]);
				$inn .= $this->toStringByPath("$path/$name($id)", true, $dep+1) . $txtpiece;
			} else {
				$inn.= trim($val);
			}
			
		}
		if ($self) {
			$txt .="$tab<".strtolower($this->nodes[$path]["name"]);
			reset($this->nodes[$path]["attributes"]);
			while ( list( $key, $val ) = each($this->nodes[$path]["attributes"]) ) {
				$quot = strpos($val, "\"") ? "'" : "\"" ;
				$txt .= " ".strtolower($key)."=$quot$val$quot";
			}
			if ($inn) {
				$txt .=">$inn$tab</".strtolower($this->nodes[$path]["name"]).">";
			} else {
				$txt .="/>";
			}
		} else {
			if ($inn) {
				$txt.="$inn";
			}
		}

		return $txt;

	}

	function toString($self=false, $dep=0) {
		return $this->toStringByPath($this->name, $self, $dep);
	}


	function getAttribute($path, $att) {
		@$res =  $this->nodes[$path]["attributes"][strtoupper($att)];
		return isset($res) ? $res : false;
	}

	function getEltByPath($path) {

		$last_slash = strrpos($path, "/");
		switch ($path[$last_slash+1]) {
			case "@": 
				return $this->getAttribute(substr($path, 0, $last_slash), substr($path, $last_slash+2));
			case "*" :
				return 	$this->toStringByPath(substr($path, 0, $last_slash), true);
			default :
				return $this->toStringByPath($path);		
		}
	}



	function setAttribute($path, $att, $val) {
		$this->nodes[$path]["attributes"][strtoupper($att)] = $val;
		return $this;
	}


	function setNode($path, $value) {

		reset($this->nodes);
		while ( list( $key, $val ) = each( $this->nodes ) ) {
			if (strpos(" ".$key, $path) == 1) {
				unset ($this->nodes[$key]);
			}
		}

		reset($value->nodes);
		while ( list( $key, $val ) = each( $value->nodes ) ) {
			if ($key) {
				if ($pos = strpos($key, "/", 1))  {
					$rel_path = "/".substr($key, $pos+1);
				} else {
					$rel_path= "";
				}
				
				$this->nodes["$path"."$rel_path"] = $val; 
			}
		}
		return $this;

	}


	function setEltByPath($path, $value) {

		$last_slash = strrpos($path, "/");

		switch ($path[$last_slash+1]) {
			case "@": 
				return $this->setAttribute(substr($path, 0, $last_slash), substr($path, $last_slash+2), $value);
			case "*" :
				return 	$this->setNode(substr($path, 0, $last_slash), $value);
			default :
				return $this->setNode($path, $value);		
		}
	}

	function save() {
		$fxml = fopen($this->filename, "w");
		fwrite($fxml, "<?xml version=\"1.0\" encoding='ISO-8859-1'?>\n");
		fwrite($fxml, $this->toString(true));
		fclose($fxml);
	}

	function element_type($path) {
		if (strpos($path, "@")) {
			return "attribute";
		} else {
			return "element";
		}
	}

	function element_name($path) {
		$path = substr($path, strrpos($path, "/"));
		return substr($path, 1, strpos($path, "(")-1);
	}


}


}

?>

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us