note 60122 added to ref.xml
| From: | php-general at lists dot php dot net | Date: | Tue, 27 Dec 2005 13:21:30 +0000 |
| Subject: | note 60122 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100784@lists.php.net to get a copy of this message | ||
i've been looking for some code wich makes me a well usable array, from "any" xml.
but though here are extremely good implementations of parsers i havnt found it, so i made up a class
wich makes an easy useable array. probably not rockstable but well formed xml worx (well it should)
<?php
ini_set("display_errors","1");
class q{
//builds ARRAY from XML
function xmltoarray($xml)
{
$p = xml_parser_create(); //he does the real work
//eventually parser could be created on construct
xml_parse_into_struct($p, $xml, $vals);
xml_parser_free($p);
$erg=array();
$pos=0;
$newcdata=array();
//print_r($vals);
return $this->valtoar($vals,$pos,$newcdata);
}
//builds ARRAY from VALS recived from parser
function valtoar(&$val,&$pos,&$cdata)
{
$erg=array(); //result
while($val[$pos]['type']!="close") //if we came 2 close!!!LOL!!! end*/
{
if($val[$pos]['type']=="open"){ //an open occured
$tag=$val[$pos]['tag']; //get the tag
if(!isset($erg[$tag])) //build the array if not yet used, wouldnt be neccessarry
$erg[$tag]=array();
$tagi = count($erg[$tag]); //get the last pos +1
if(isset($val[$pos]['attributes'])) //add attributes
$erg[$tag][$tagi]['attributes']=$val[$pos]['attributes'];
else
$erg[$tag][$tagi]['attributes']=array();
$newcdata=array();
if(isset($val[$pos]['value'])) //add value
$newcdata[]=$val[$pos]['value'];
$pos++; // next pos // get all ellements
$erg[$tag][$tagi]['elements']=$this->valtoar($val,$pos,$newcdata);
$erg[$tag][$tagi]['values']=$newcdata; //all cdata found is set to
[tag][#][values][#]=>string
if($val[$pos]['level']!=1) //there is only one level 1 and it must take
our cancel contition
$pos++;
}
elseif($val[$pos]['type']=="cdata"){ //handle the cdata to be
referencial returned in open tag
$valuesi = count($cdata);
$cdata[$valuesi]=$val[$pos]['value'];
$pos++;
}
elseif($val[$pos]['type']=="complete"){ //complete tags are add
$tag=$val[$pos]['tag'];
if(!isset($erg[$tag])) // add the tag
$erg[$tag]=array();
$tagi = count($erg[$tag]);
if(isset($val[$pos]['attributes'])) //add the attributes
$erg[$tag][$tagi]['attributes']=$val[$pos]['attributes'];
else
$erg[$tag][$tagi]['attributes']=array();
if(isset($val[$pos]['value']))
$erg[$tag][$tagi]['values']=array($val[$pos]['value']);
else
$erg[$tag][$tagi]['values']=array();
$erg[$tag][$tagi]['elements']=array();
$pos++;
}
}
return $erg;
}
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60122
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60122&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60122&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60122&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60122&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60122&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60122&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60122&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60122&report=yes
Search -- http://master.php.net/manage/user-notes.php