note 71493 added to ref.domxml

From: Date: Fri, 01 Dec 2006 16:05:52 +0000
Subject: note 71493 added to ref.domxml
Groups: php.notes 
Request: Send a blank email to php-notes+get-120220@lists.php.net to get a copy of this message
Referenced array functions drive me crazy for one reason or another (personal issue I guess). So for any others like me, here's my modification (thanks to the original posters below for the base to work on!) I haven't tested this on much more than simple XML files, so there's probably a few ways to break this - I'm also thinking this could probably be rewritten to be more efficient also, but it's working quite well for me thus far. <?php function xml2array($domnode) { $nodearray = array(); $domnode = $domnode->firstChild; while (!is_null($domnode)) { $currentnode = $domnode->nodeName; switch ($domnode->nodeType) { case XML_TEXT_NODE: if(!(trim($domnode->nodeValue) == "")) $nodearray['cdata'] = $domnode->nodeValue; break; case XML_ELEMENT_NODE: if ($domnode->hasAttributes() ) { $elementarray = array(); $attributes = $domnode->attributes; foreach ($attributes as $index => $domobj) { $elementarray[$domobj->name] = $domobj->value; } } break; } if ( $domnode->hasChildNodes() ) { $nodearray[$currentnode][] = xml2array($domnode); if (isset($elementarray)) { $currnodeindex = count($nodearray[$currentnode]) - 1; $nodearray[$currentnode][$currnodeindex]['@'] = $elementarray; } } else { if (isset($elementarray) && $domnode->nodeType != XML_TEXT_NODE) { $nodearray[$currentnode]['@'] = $elementarray; } } $domnode = $domnode->nextSibling; } return $nodearray; } ?> ---- Server IP: 216.194.113.175 Probable Submitter: 68.93.45.45 ---- Manual Page -- http://www.php.net/manual/en/ref.domxml.php Edit -- https://master.php.net/note/edit/71493 Del: integrated -- https://master.php.net/note/delete/71493/integrated Del: useless -- https://master.php.net/note/delete/71493/useless Del: bad code -- https://master.php.net/note/delete/71493/bad+code Del: spam -- https://master.php.net/note/delete/71493/spam Del: non-english -- https://master.php.net/note/delete/71493/non-english Del: in docs -- https://master.php.net/note/delete/71493/in+docs Del: other reasons-- https://master.php.net/note/delete/71493 Reject -- https://master.php.net/note/reject/71493 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120220) next »