Fonction PHP XML

Résolu
Bonjour,

Je cherche à comprendre cette fonction :

function & parse(&$data){
		$this->document = array();
		$this->stack    = array();
		$this->parent   = &$this->document;
		return xml_parse($this->parser, $data, true) ? $this->document : NULL;
	}

c'est surtout la derniere ligne, je ne trouve pas de documentation dessus, sur le '?' et le ':' !!!

Donc si quelqu'un pouvait m'éclairer sur cette ligne, svp!!!
Configuration: Windows 2000
Firefox 3.5.2

11 réponses

  1. Ça ressemble à un if javascript en forme courte ...
    0
    1. @benviiCa correspond à ceci :

      if(xml_parse($this->parser, $data, true))
      { return($this->document); }
      else
      { return(NULL);}
      0
    2. @benviimerci...
      0
  2. disons que la dernière ligne, me retourne l'erreur :

    Notice: Only variable references should be returned by reference in C:\Program Files\EasyPHP 3.0\www\testData\xml.php on line 73

    donc ça me bloque parce que je ne vois pas du tout comment y remédier...
    0
    1. Il faudrait plus de code et aussi quel est le langage de ta fonction.

      Je ne suis pas en mesure de te répondre avec ce que tu as mis.
      0
      1. excuse

        voici la grande parti du code,

        ###################################################################################
        # XML class: utility class to be used with PHP's XML handling functions
        ###################################################################################
        class XML{
        	var $parser;   #a reference to the XML parser
        	var $document; #the entire XML structure built up so far
        	var $parent;   #a pointer to the current parent - the parent will be an array
        	var $stack;    #a stack of the most recent parent at each nesting level
        	var $last_opened_tag; #keeps track of the last tag opened.
        
        	function XML(){
         		$this->parser = &xml_parser_create();
        		xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
        		xml_set_object($this->parser, $this);
        		xml_set_element_handler($this->parser, 'open','close');
        		xml_set_character_data_handler($this->parser, 'data');
        	}
        	function destruct(){ xml_parser_free($this->parser); }
        	function & parse(&$data){
        		$this->document = array();
        		$this->stack    = array();
        		$this->parent   = &$this->document;
        		return xml_parse($this->parser, $data, true) ? $this->document : NULL;
        	}
        	function open(&$parser, $tag, $attributes){
        		$this->data = ''; #stores temporary cdata
        		$this->last_opened_tag = $tag;
        		if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
        			if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
        				#this is the third or later instance of $tag we've come across
        				$key = count_numeric_items($this->parent[$tag]);
        			}else{
        				#this is the second instance of $tag that we've seen. shift around
        				if(array_key_exists("$tag attr",$this->parent)){
        					$arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
        					unset($this->parent["$tag attr"]);
        				}else{
        					$arr = array(&$this->parent[$tag]);
        				}
        				$this->parent[$tag] = &$arr;
        				$key = 1;
        			}
        			$this->parent = &$this->parent[$tag];
        		}else{
        			$key = $tag;
        		}
        		if($attributes) $this->parent["$key attr"] = $attributes;
        		$this->parent  = &$this->parent[$key];
        		$this->stack[] = &$this->parent;
        	}
        	function data(&$parser, $data){
        		if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
        			$this->data .= $data;
        	}
        	function close(&$parser, $tag){
        		if($this->last_opened_tag == $tag){
        			$this->parent = $this->data;
        			$this->last_opened_tag = NULL;
        		}
        		array_pop($this->stack);
        		if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
        	}
        }
        function count_numeric_items(&$array){
        	return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
        }
        ?>
        
        


        J'ai pas jugé utile de te mettre la fonction "function & XML_unserialize(&$xml)" et "function & XML_serialize(&$data, $level = 0, $prior_key = NULL){" mais si tu penses en avoir besoin je te le rajoute.
        c'est codé en php, il me semble...

        Merci en tout cas
        0
        1. Je peux pas t'aider, mais avec ce code, d'autres le pourront surement. Désolé, je suis pas un pro en XML.
          0
          1. c'est pas grave!!!! arf arf arf, marde!!!!! m'enfin bon!!! merci quand même, j'espère que quelqu'un pourra répondre!!!!
            0
            1. je m'arrache les cheveux!!!!
              0
              1. Je montre mes 3pages principales ou plutôt les pages qui me semblent utile pour voir d'où vient le probleme...

                xmlparser.php

                <?php
                
                class XMLParser {
                 var $filename;
                 var $xml;
                 var $data;
                 
                 function XMLParser($xml_file)
                 {
                 $this->filename = $xml_file;
                 $this->xml = xml_parser_create();
                 xml_set_object($this->xml, $this);
                 xml_set_element_handler($this->xml, 'startHandler', 'endHandler');
                 xml_set_character_data_handler($this->xml, 'dataHandler');
                 $this->parse($xml_file);
                 }
                 
                 function parse($xml_file)
                 {
                 if (!($fp = fopen($xml_file, 'r'))) {
                 die('Cannot open XML data file: '.$xml_file);
                 return false;
                 }
                
                 $bytes_to_parse = 512;
                
                 while ($data = fread($fp, $bytes_to_parse)) {
                 $parse = xml_parse($this->xml, $data, feof($fp));
                 
                 if (!$parse) {
                 die(sprintf("XML error: %s at line %d",
                 xml_error_string(xml_get_error_code($this->xml)),
                 xml_get_current_line_number($this->xml)));
                 xml_parser_free($this->xml
                 );
                 }
                 }
                
                 return true;
                 }
                 
                 function startHandler($parser, $name, $attributes)
                 {
                 //$data['DATA'] = $name;
                 if ($attributes) { $data['ATTR'] = $attributes; }
                 $this->data[] = $data;
                 }
                
                 function dataHandler($parser, $data)
                 {
                 if ($data = trim($data)) {
                 $index = count($this->data) - 1;
                 // begin multi-line bug fix (use the .= operator)
                 $this->data[$index]['VAL'] .= $data;
                 // end multi-line bug fix
                 }
                 }
                
                 function endHandler($parser, $name)
                 {
                 if (count($this->data) > 1) {
                 $data = array_pop($this->data);
                 $index = count($this->data) - 1;
                 $this->data[$index]['CHILD'][] = $data;
                 }
                 }
                }
                ?>
                


                xml.php

                ###################################################################################
                # XML_unserialize: takes raw XML as a parameter (a string)
                # and returns an equivalent PHP data structure
                ###################################################################################
                function & XML_unserialize(&$xml){
                	$xml_parser = &new XML();
                	$data = &$xml_parser->parse($xml);
                	$xml_parser->destruct();
                	return $data;
                }
                ###################################################################################
                # XML_serialize: serializes any PHP data structure into XML
                # Takes one parameter: the data to serialize. Must be an array.
                ###################################################################################
                function & XML_serialize(&$data, $level = 0, $prior_key = NULL){
                	if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
                	while(list($key, $value) = each($data))
                		if(!strpos($key, ' attr')) #if it's not an attribute
                			#we don't treat attributes by themselves, so for an empty element
                			# that has attributes you still need to set the element to NULL
                
                			if(is_array($value) and array_key_exists(0, $value)){
                				XML_serialize($value, $level, $key);
                			}else{
                				$tag = $prior_key ? $prior_key : $key;
                				echo str_repeat("\t", $level),'<',$tag;
                				if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
                					while(list($attr_name, $attr_value) = each($data["$key attr"]))
                						echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
                					reset($data["$key attr"]);
                				}
                
                				if(is_null($value)) echo " />\n";
                				elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
                				else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
                			}
                	reset($data);
                	if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
                }
                ###################################################################################
                # XML class: utility class to be used with PHP's XML handling functions
                ###################################################################################
                class XML{
                	var $parser; #a reference to the XML parser
                	var $document; #the entire XML structure built up so far
                	var $parent; #a pointer to the current parent - the parent will be an array
                	var $stack; #a stack of the most recent parent at each nesting level
                	var $last_opened_tag; #keeps track of the last tag opened.
                	
                
                	function XML(){
                 		$this->parser = &xml_parser_create();
                		xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
                		xml_set_object($this->parser, $this);
                		xml_set_element_handler($this->parser, 'open','close');
                		xml_set_character_data_handler($this->parser, 'data');
                	}
                	function destruct(){ xml_parser_free($this->parser); }
                	function & parse(&$data){
                		$this->document = array();
                		$this->stack = array();
                		$this->parent = &$this->document;
                		return xml_parse($this->parser, $data, true) ? $this->document : $null;
                		
                	}
                	function open(&$parser, $tag, $attributes){
                		$this->data = ''; #stores temporary cdata
                		$this->last_opened_tag = $tag;
                		if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
                			if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
                				#this is the third or later instance of $tag we've come across
                				$key = count_numeric_items($this->parent[$tag]);
                			}else{
                				#this is the second instance of $tag that we've seen. shift around
                				if(array_key_exists("$tag attr",$this->parent)){
                					$arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
                					unset($this->parent["$tag attr"]);
                				}else{
                					$arr = array(&$this->parent[$tag]);
                				}
                				$this->parent[$tag] = &$arr;
                				$key = 1;
                			}
                			$this->parent = &$this->parent[$tag];
                		}else{
                			$key = $tag;
                		}
                		if($attributes) $this->parent["$key attr"] = $attributes;
                		$this->parent = &$this->parent[$key];
                		$this->stack[] = &$this->parent;
                	}
                	function data(&$parser, $data){
                		if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
                			$this->data .= $data;
                	}
                	function close(&$parser, $tag){
                		if($this->last_opened_tag == $tag){
                			$this->parent = $this->data;
                			$this->last_opened_tag = NULL;
                		}
                		array_pop($this->stack);
                		if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
                	}
                }
                function count_numeric_items(&$array){
                	return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
                }
                ?>
                


                et newdata.php

                <?PHP
                	include('secure.php');	
                	include('xmlparser.php');
                	include('xml.php');
                	no_cache();
                
                	// CONNEXION MYSQL
                	connexion();
                	
                	$id = $_GET["id"];
                	
                	echo "<span style='font-family : verdana; font-size : 12px;'>";
                	if (!empty($gfullname))
                		echo "Bonjour $gfullname";
                	else
                		echo "Bonjour $gpseudo";
                		
                	echo "</span>";	
                	echo "<hr size=0><br>";
                		
                	// On recherche tous les établissements installés pour ce client
                	$sRequete = "select * from licence where pseudo='".$gpseudo."'";
                	$results=requete($sRequete);
                	
                	// Si aucune établissement n'est trouvé
                	if(mysql_num_rows($results)==0)
                	{		
                		echo "<span style='font-family : verdana; font-size : 12px;'>";
                		echo "Aucun établissement à afficher";		
                		echo "</span>";
                		echo "<br><br>";
                		echo "<input type='button' onclick='javascript:document.location.reload();' value='Actualiser'>";						
                	}
                	else 
                	{
                		echo "</span>";
                		echo "<span style='font-family : verdana; font-size : 25px;'>Votre tableau de bord d'aujourd'hui - Orchestra Web Manager</span>";		
                		echo "<br><br>";
                		echo '<table border="0" cellpadding="5" cellspacing="1" bgcolor="#999999" style="font-family : verdana; font-size : 12px;">';
                		echo '<tr valign="top"><td style="font-weight:bold;" bgcolor="#DDDDDD">Données de gestion</td>';		
                		
                		$nNbFichiers = 0;
                		
                		$aDay = array(1 =>"Lundi", 2 =>"Mardi", 3 =>"Mercredi", 4 =>"Jeudi", 5 =>"Vendredi", 6 =>"Samedi", 7 =>"Dimanche");	
                		$dayDate = date("l");
                		echo $dayDate;
                		 
                		// AFFICHAGE DE LA LIGNE TITRES
                		for($i=0; $i<mysql_num_rows($results); $i++)
                		{
                		 $sLicence = mysql_result($results,$i,"cleident");
                		 $sNom = mysql_result($results,$i,"nom");
                		 
                			// Lecture des données ([licence].xml)
                			$sFileName = "upload/".$sLicence."/".$dayDate.".xml";
                			
                			if (file_exists($sFileName))
                			{
                				$sJour = $aDay[date("w", filemtime($sFileName))];
                				
                				// Ajoute la date et l'heure du fichier
                			 	$sTime = $sJour.", ".date("d/m/Y H:i", filemtime($sFileName));
                			 				 
                		 	echo "<td align='right' bgcolor='#DDDDDD' style='padding :5px;' nowrap>";
                		 	echo "<b>$sNom</b>";
                		 	echo "<br>";
                		 	echo "<span style='font-size : 10px;'>";
                		 	echo $sTime;
                		 	echo "</span>";
                		 	echo "</td>";
                		 	
                		 	$nNbFichiers++;
                			}
                			
                			
                		}
                		
                		if ($nNbFichiers>1)
                			echo "<td align='right' width='100' bgcolor='#DD3333' style='color: #FFFFFF; font-weight:bold; padding :5px;' nowrap>Totaux</td>";
                		
                		echo "</tr>";
                
                		// MEMORISATION DES TITRES ET VALEURS
                		$asTitres = array();
                		$asValeurs = array();
                		
                		// On récupère les titres et les valeurs
                		ChargeTableaux($results, $asTitres, $asValeurs);
                		
                		// Si aucun fichier n'a été trouvé
                		if (empty($asTitres))
                		{	
                			echo "</table><br><br><span style='font-family : verdana; font-size : 12px;'>Aucunes données n'a été transmises pour le moment</span>";
                			echo "<br><br>";
                			echo "<input type='button' onclick='javascript:document.location.reload();' value='Actualiser'>";						
                		}
                		else
                		{		
                			// AFFICHAGE DE CHAQUE LIGNE
                			$nTotalLignes = count($asTitres);				
                			for ($j=0;$j<$nTotalLignes; $j++)
                			{
                				$sTitre = $asTitres[$j];
                					
                				if ($j%2>0)
                					$sBGColor = "#F5F5F5";
                				else
                					$sBGColor = "#FFFFFF";
                					
                				echo "<tr><td bgcolor='$sBGColor' style='padding :5px;' nowrap>".$sTitre."</td>";
                				
                				$rTotal = 0;
                				foreach($asValeurs[$j] as $rValue)
                				{
                					$rTotal += $rValue;
                					
                					echo "<td align='right' style='padding :5px;' bgcolor='$sBGColor'>".$rValue."</td>";				
                				}
                				
                				if ($nNbFichiers > 1)
                				{
                					$sTotal = sprintf("%.02f", $rTotal);
                									
                					echo "<td align='right' style='padding :5px;' bgcolor='#FFCCCC'>".$sTotal."</td>";
                				}
                				
                				echo "</tr>";
                			}
                			
                			if ($nNbFichiers>1)
                				$nCols = $nNbFichiers+2;
                			else
                				$nCols = $nNbFichiers+1;
                			
                			echo "<tr><td align='right' colspan=".($nCols)." bgcolor=#999999>";
                			echo "<input type='button' onclick='javascript:document.location.reload();' value='Actualiser'>";				
                			echo "</td></tr>";
                			
                			echo "</table>";
                		}
                	}
                	echo "<br>";
                	echo "<a style='font-family : verdana; font-size : 12px;' target='_blank' href=\"dateprecedente.php?login=1&id=".$id."&licence=".$sLicence."\">voir les dates précédentes</a>";
                	echo "<br><br><hr size=0>";
                	//double / à sup	
                	//echo "[ <a style='font-family : verdana; font-size : 12px;' href=\"javascript:parent.location.href='http://www.orchestra-software.com'\">Se déconnecter</a> ]";
                		echo "<a style='font-family : verdana; font-size : 12px;' href=\"login.php?login=1&id=".$id."\">Se déconnecter</a>";
                	
                	function ExtraitItem($paArray, $psBlocName)
                	{		
                		while (list($sKey, $aValue) = each($paArray))
                		{
                			if (strtolower($sKey) == $psBlocName)
                			{
                				return $aValue;
                			}
                		}
                		
                		return null;
                	}
                	
                	function ChargeTableaux($pRequest, &$pasTitres, &$pasValeurs)
                	{		
                		$bFirst = true;
                				
                		// On utilise le premier fichier pour récupérer la liste des titres
                		// Pour chaque établissement, on affiche les titres
                		for($i=0; $i<mysql_num_rows($pRequest); $i++)
                		{
                			$sLicence = mysql_result($pRequest, $i,"cleident");
                			$dayDate = date("l");
                			// Lecture des données ([licence].xml)
                			$sFileName = "upload/".$sLicence."/".$dayDate.".xml";
                			if (file_exists($sFileName))
                			{				
                				$bAtLeastOne = true;
                		
                				// On charge le contenu du fichier	
                				$sContent = file_get_contents($sFileName);
                				$aData = XML_unserialize($sContent);
                						
                				// On extrait le bloc principal et les titres
                				$aBloc = ExtraitItem($aData, "bloc");
                				$aTitres = ExtraitItem($aBloc, "titres");
                				$aValeurs = ExtraitItem($aBloc, "valeurs");
                				
                				if ($bFirst)
                				{					
                					// On récupère tous les titres et les stoque dans le tableau $pasTitres
                					$j=1;
                					$sTitre = ExtraitItem($aTitres, "titre".$j);
                					while ($sTitre != null)
                					{
                						$pasTitres[] = $sTitre;
                						$j++;
                						$sTitre = ExtraitItem($aTitres, "titre".$j);
                					}
                					
                					$bFirst = false;
                				}
                				
                				// On récupère toutes les valeurs et les stoque dans le tableau $pasValeurs
                				$j=1;
                				$sValeur = ExtraitItem($aValeurs, "valeur".$j);
                				while ($sValeur != null)
                				{
                					$pasValeurs[$j-1][] = str_replace(",", ".", $sValeur);
                					$j++;
                					$sValeur = ExtraitItem($aValeurs, "valeur".$j);
                				}						
                			}
                			
                		}
                	}
                ?>


                En espérant que quelqu'un puisse se pencher dessus..svp svp svp svp svp svp svp
                0
                1. Je pense pas que ça marche, mais essayes de changer cette ligne :
                  return xml_parse($this->parser, $data, true) ? $this->document : NULL;

                  En :
                  return (xml_parse($this->parser, $data, true)=="1") ? $this->document : NULL;

                  Ou :
                  return (xml_parse($this->parser, $data, true)==1) ? $this->document : NULL;

                  D'après la doc la fonction xml_parse (je crois que c'est le parseur xml), retourne 1 ou 0.

                  La doc -> https://www.php.net/manual/fr/function.xml-parse.php
                  0
                  1. merci pour ta réponse...mais ça ne m'enlève pas les "notice"...la raaaaaaaaaaaaaaaaaaaaage!!!! même avec la doc je sèche...
                    0
                    1. en faite, j'ai enlevé le & de function & parse!! et plus de notice!!! voilààààààààààà bon par contre, je ne sais pas à quoi ça servait...mais y'a plus!!! lol
                      0