note 82962 added to simplexml.examples

From: Date: Sat, 03 May 2008 16:55:02 +0000
Subject: note 82962 added to simplexml.examples
Groups: php.notes 
Request: Send a blank email to php-notes+get-139030@lists.php.net to get a copy of this message
<?php class QXML { const attribute = "@attributes"; const textNode = "@textNode"; protected $xml; public function asObject(){ if(strlen($this->asXML())){ return simplexml_load_string($this->asXml()); } else { return false; } } public function setXml($xml){ $this->xml = $xml; } public function asXML(){ return $this->xml; } public function toArray($xml) { $results = Array(); foreach($xml->children() as $key => $object) { $recObj = $this->toArray($object); foreach( $object->attributes() as $ak => $av ) { $recObj[self::attribute][$ak] = (string)$av; } if(!$recObj[self::attribute]){ $recObj[self::attribute] = ""; } if(!$recObj[self::textNode]){ $recObj[self::textNode] = trim((string)$object); } if (!in_array($key,array_keys($results))) { $results[$key] = $recObj; } elseif (@in_array(0,@array_keys($results[$key]))) { if(count($results[$key])==2){ if($results[$key][self::attribute] || $results[$key][self::textNode] ) { $tmp=Array(); $tmp[self::attribute] = $results[$key][self::attribute]; $tmp[self::textNode] = $results[$key][self::textNode]; unset($results[$key][self::attribute]); unset($results[$key][self::textNode]); $results[$key][0]=$tmp; unset($tmp); } } if(count(array_diff_key($results[$key],$recObj))==0 && count($results[$key])>0) { $tmp=$results[$key]; unset($results[$key]); $results[$key][]=$tmp; } $results[$key][] = $recObj; } else { $results[$key] = array($results[$key]); $results[$key][] = $recObj; } } return $results; } public function toXML( $xmlArray, $elmName='graph', $elmCloseTag="", $level=0) { $this->setXml( $this->_toXML( $xmlArray, $elmName, $elmCloseTag, $level) ); } } ?> ---- Server IP: 212.124.37.9 Probable Submitter: 85.220.155.143 (proxied: 85.220.155.143) ---- Manual Page -- http://www.php.net/manual/en/simplexml.examples.php Edit -- https://master.php.net/note/edit/82962 Del: integrated -- https://master.php.net/note/delete/82962/integrated Del: useless -- https://master.php.net/note/delete/82962/useless Del: bad code -- https://master.php.net/note/delete/82962/bad+code Del: spam -- https://master.php.net/note/delete/82962/spam Del: non-english -- https://master.php.net/note/delete/82962/non-english Del: in docs -- https://master.php.net/note/delete/82962/in+docs Del: other reasons-- https://master.php.net/note/delete/82962 Reject -- https://master.php.net/note/reject/82962 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139030) next »