note 86631 added to class.xmlreader

From: Date: Mon, 27 Oct 2008 10:32:20 +0000
Subject: note 86631 added to class.xmlreader
Groups: php.notes 
Request: Send a blank email to php-notes+get-145794@lists.php.net to get a copy of this message
function parseXml($xml) { $assoc = null; while($xml->read()){ switch ($xml->nodeType) { case XMLReader::END_ELEMENT: return $assoc; case XMLReader::ELEMENT: if(!isset($assoc[$xml->name])) { if($xml->hasAttributes) { $assoc[$xml->name][] = $xml->isEmptyElement ? '' : parseXml($xml); } else { if($xml->isEmptyElement) { $assoc[$xml->name] = ''; } else { $assoc[$xml->name] = parseXml($xml); } } } elseif (is_array($assoc[$xml->name])) { if($xml->hasAttributes) { $assoc[$xml->name][] = $xml->isEmptyElement ? '' : parseXml($xml); } else { if($xml->isEmptyElement) { $assoc[$xml->name][] = ''; } else { $assoc[$xml->name][] = parseXml($xml); } } } else { $mOldVar = $assoc[$xml->name]; $assoc[$xml->name] = array($mOldVar); if($xml->hasAttributes) { $assoc[$xml->name][] = $xml->isEmptyElement ? '' : parseXml($xml); } else { if($xml->isEmptyElement) { $assoc[$xml->name][] = ''; } else { $assoc[$xml->name][] = parseXml($xml); } } } if($xml->hasAttributes){ $el =& $assoc[$xml->name][count($assoc[$xml->name]) - 1]; while($xml->moveToNextAttribute()) { $el[$xml->name] = $xml->value; } } break; case XMLReader::TEXT: case XMLReader::CDATA: $assoc .= $xml->value; } } return $assoc; } my own implementation of XmlParser based od existing code. Function is creating easy to use arrays ;) Try it.. ---- Server IP: 82.160.94.10 Probable Submitter: 87.205.136.218 ---- Manual Page -- http://www.php.net/manual/en/class.xmlreader.php Edit -- https://master.php.net/note/edit/86631 Del: integrated -- https://master.php.net/note/delete/86631/integrated Del: useless -- https://master.php.net/note/delete/86631/useless Del: bad code -- https://master.php.net/note/delete/86631/bad+code Del: spam -- https://master.php.net/note/delete/86631/spam Del: non-english -- https://master.php.net/note/delete/86631/non-english Del: in docs -- https://master.php.net/note/delete/86631/in+docs Del: other reasons-- https://master.php.net/note/delete/86631 Reject -- https://master.php.net/note/reject/86631 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145794) next »