note 60123 deleted from ref.xml by nlopess
| From: | nlopess@php.net | Date: | Tue, 27 Dec 2005 14:44:22 +0000 |
| Subject: | note 60123 deleted from ref.xml by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-100791@lists.php.net to get a copy of this message | ||
Note Submitter:
----
<?php
there should be more code in front of this......
function arraytoxml($cjxml,$depth=0)
{
$out="";
foreach($cjxml as $k => $pv){
foreach($pv as $v){
if($depth>=0){
$out.="\n";
for($i=0;$i<$depth;$i++)
$out.="\t";
}
$out.="<".$k;
foreach($v['attributes'] as $ak =>$av)
{
$out.=' '.$ak.'="'.$av.'"';
}
$out.=">";
if($depth>=0)
$out.=$this->arraytoxml($v['elements'],$depth+1);
else
$out.=$this->arraytoxml($v['elements'],-1);
foreach($v['values'] as $vv)
{
if(strpos($vv,"]]>")===false)
$vvo="<![CDATA[".$vv."]]>";
else
$vvo=str_replace(array(">","<","'",'"','&'),
array(">","<","'",'"','&'),$vv);
$out.=$vvo;
}
$out.="</".$k.">";
}
}
return $out;
}
}
$ix=new q();
$erg =$ix->xmltoarray('<object name="name2">alphatext?
<element name="name" id="1"><![CDATA[text abc <de
]]></element>
objettext2<element name="name" id="2">text2
</element><element name="name" id="3" /><!-- any -->
objekttext</object>');
print_r($erg);
$ergt=$ix->arraytoxml($erg);
print($ergt);
$ergi=$ix->arraytoxml($erg,-1);
print($ergi);
$erg =$ix->xmltoarray($ergi);
print_r($erg);
//it produes an array wich can be easiely used by programs without any special cases
//its structure is
/*
[tag]
[#]
["attributes"]
[attribute]=>string
["values"]
[#]=>string
["elements"]
[tag]
[#]
["attributes"]
[attribute]=>string
["values"]
[#]=>string
["elements"]
[tag]
[#]
.......
# means any number
tag means the tag of the element
attribut means the attribut
.......
*/
//btw. array 2 xml builds the xml from this array, u see the code is simple since the
array has no special cases
?>