note 100603 added to simplexmlelement.children

From: Date: Tue, 26 Oct 2010 12:19:12 +0000
Subject: note 100603 added to simplexmlelement.children
Groups: php.notes 
Request: Send a blank email to php-notes+get-173464@lists.php.net to get a copy of this message
Here's my xml-to-array routine. Unlike many of the other comments here, this one takes namespaces into account. <?php class XmlArray { public function load_dom ($xml) { $node=simplexml_import_dom($xml); return $this->add_node($node); } public function load_string ($s) { $node=simplexml_load_string($s); return $this->add_node($node); } private function add_node ($node, &$parent=null, $namespace='', $recursive=false) { $namespaces = $node->getNameSpaces(true); $content="$node"; $r['name']=$node->getName(); if (!$recursive) { $tmp=array_keys($node->getNameSpaces(false)); $r['namespace']=$tmp[0]; $r['namespaces']=$namespaces; } if ($namespace) $r['namespace']=$namespace; if ($content) $r['content']=$content; foreach ($namespaces as $pre=>$ns) { foreach ($node->children($ns) as $k=>$v) { $this->add_node($v, $r['children'], $pre, true); } foreach ($node->attributes($ns) as $k=>$v) { $r['attributes'][$k]="$pre:$v"; } } foreach ($node->children() as $k=>$v) { $this->add_node($v, $r['children'], '', true); } foreach ($node->attributes() as $k=>$v) { $r['attributes'][$k]="$v"; } $parent[]=&$r; return $parent[0]; } } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 69.254.213.103 ---- Manual Page -- http://www.php.net/manual/en/simplexmlelement.children.php Edit -- https://master.php.net/note/edit/100603 Del: integrated -- https://master.php.net/note/delete/100603/integrated Del: useless -- https://master.php.net/note/delete/100603/useless Del: bad code -- https://master.php.net/note/delete/100603/bad+code Del: spam -- https://master.php.net/note/delete/100603/spam Del: non-english -- https://master.php.net/note/delete/100603/non-english Del: in docs -- https://master.php.net/note/delete/100603/in+docs Del: other reasons-- https://master.php.net/note/delete/100603 Reject -- https://master.php.net/note/reject/100603 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173464) next »