Location: PHPKode > projects > LDAPted > ldapted/includes/XMLDocument.php
<?php
/***************************************************************************
 *
 *                                XMLDocument.php
 *                              -------------------
 *
 *   begin                : Friday, Jul 5, 2002
 *   copyright            : (C) 2002 The Kabramps Team
 *   email                : hide@address.com,
 *                          hide@address.com
 *
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *   See the GNU General Public License for more details.
 *   (http://www.gnu.org/licenses/gpl.html)
 *
 ***************************************************************************/
$php_version = phpversion();
$php_version = $php_version[0]; // treat string as array - power to the script languages

if( $php_version == 5 )
{
	class XMLDocument
	{
	  
		var $context;
		  
		function XMLDocument()
		{}
	
		function init($filename)
		{
			$doc = new DOMDocument;
			$doc->preserveWhiteSpace = false;
			$doc->Load( $filename );
		    $this->context=new DOMXPath($doc);
		}
	
		function get_xml_attributes($xpath, $attribute)
		{
			// returns an array of attributes selected by the given xpath
			$return = array();
			$result=$this->context->query( $xpath );
			foreach( $result as $entry )
			{
				if ( is_array($attribute) )
				{
					$tmp = array();
					for  ( $j=0; $j < count($attribute); $j++ )
					{
						$tmp[$attribute[$j]] = $entry->getAttribute($attribute[$j]);
					}
					$return[] = $tmp;
				}
				else
				{
					$return[] = $entry->getAttribute($attribute);
				}
			}
			return $return;
		}
	
		// returns an array of contents selected by the given xpath
		function get_xml_content($xpath,$start=0,$count=null)
		{
			if ( $count )
			{
				$end = $start + $count;
			}
			$return = array();
			$result=$this->context->query( $xpath );
	
			for  ( $i=$start; $i < count($result->length); $i++ )
			{
				if ( $i == $end )
				{
					//$i = count($result->nodeset);
				}
				else
				{
					$temp = $result->item($i);
					$return[] = $temp->nodeValue;
				}
			}
			return $return;
		}
		
	
		function get_xml_content_by_key( $xpath , $key )
		{
			$return = array();
			$result=$this->context->query( $xpath );
			foreach( $result as $entry )
			{
				$return[$entry->getAttribute($key)] = $entry->nodeValue;
			}
			return $return;
		}
	
	
		function get_xml_tags($xpath)
		{
			$return = array();
		    
			$result = $this->context->query( $xpath );
	
			foreach( $result as $entry )
			{
				$return[] = $entry->tagName;
			}
			return $return;
		}
	}	
}
else
{
	// This class depends on the DOMXML extension >= 2.4.30
	// Older versions of this extions won't work.
	if (!extension_loaded('domxml'))
	{
		die(gettext("DOMXML extension (>=2.4.30) is not available in your PHP version - so we quit here!"));
	}
	class XMLDocument {
	  
	  var $context;
	  
	  function XMLDocument() {
	  }
	  
	  function init($filename) {
	    // new use of the internal php4 xml parser
	    $filebuffer = fopen ($filename,"r");
	    if ($filebuffer) {
	      $content = fread($filebuffer, filesize($filename));
	    }
	    fclose($filebuffer);
	    
	    $xml=$content;
	    $doc=xmldoc($xml);
	    $this->context=@xpath_new_context($doc);
	  }
	  
	  function get_xml_attributes($xpath, $attribute) {
	    // returns an array of attributes selected by the given xpath 
	    $return = array();
	    $result=@xpath_eval($this->context,$xpath);
	    for  ( $i=0; $i<count($result->nodeset); $i++ ) {
	      if ( is_array($attribute) ) {
	        $tmp = array();
	        for  ( $j=0; $j < count($attribute); $j++ ) {
	          $tmp[$attribute[$j]] = $result->nodeset[$i]->get_attribute($attribute[$j]);
	        }
	        $return[] = $tmp;
	      } else {
	        $return[] = $result->nodeset[$i]->get_attribute($attribute);
	      }
	    }
	    return $return;
	  }
	  
	  function get_xml_content($xpath,$start=0,$count=null) {
	    // returns an array of contents selected by the given xpath
	    if ( $count ) {
	      $end = $start + $count;
	    }
	    $return = array();
	    $result=@xpath_eval($this->context,$xpath);
	    for  ( $i=$start;$i<count($result->nodeset); $i++ ) {
	      if ( $i == $end ) {
		$i = count($result->nodeset);
	      } else {
		$return[] = utf8_decode($result->nodeset[$i]->get_content());
	      }
	    }
	    return $return;
	  }
	
	  function get_xml_content_by_key($xpath,$key) {
	    // returns an array of keys of contents selected by the given xpath
	    $return = array();
	    $result=@xpath_eval($this->context,$xpath);
	    for  ( $i=0;$i<count($result->nodeset); $i++ ) {
	      $return[$result->nodeset[$i]->get_attribute($key)] = utf8_decode($result->nodeset[$i]->get_content());
	    }
	    return $return;
	  }
	
	  function get_xml_tags($xpath) {
	    $return = array();
	    $result=@xpath_eval($this->context,$xpath);
	    //echo "Xpath: ".$xpath."<br>";
	    for  ( $i=0;$i<count($result->nodeset); $i++ ) {
	      $return[] = $result->nodeset[$i]->tagname;
	      //echo "Tagname: ".$result->nodeset[$i]->tagname."<br>";
	    }
	    return $return;
	  }
	}
}


?>
Return current item: LDAPted