note 74285 added to ref.domxml

From: Date: Tue, 03 Apr 2007 15:57:16 +0000
Subject: note 74285 added to ref.domxml
Groups: php.notes 
Request: Send a blank email to php-notes+get-125055@lists.php.net to get a copy of this message
This short recursive fill a simple array in through data and meta domxml structure... <?php Class DomTree { var $tab = array(); var $domNode; function DomTree($xml) { $this->domNode = domxml_open_mem($xml); //$this->domNode = $dom->document_element(); } // >((dom)node) ((dom)node)> function getAttribute($node) { $tab=array(); foreach($node->attributes() as $k1->$v1) { $tab[$k1->{''}->name]=$k1->{''}->value; } return $tab; } function domxml2array($node,&$tab,&$i) { if($next=$node->first_child()) { do{ switch($next->node_type()) { case 3: $tab['data']=$next->node_value(); break; case 1: $tab[$next->node_name()."#".++$i]['meta'] = $this->getAttribute($next); $this->domxml2array($next,$tab[$next->node_name()."#".$i],$i); break; } }while($next=$next->next_sibling()); } return $this->tab=$tab; } } // INIT $header = "<?xml version='1.0' encoding='UTF-8' ?>\n<?xml-stylesheet type='text/xsl' href='./Template/main.xsl'?>"; $doc = '<regulation alias="regulation1"><intitule>regulation 1</intitule><title num="1">this is title 1<tmp num="1">this is tmp</tmp></title><title num="2">this is title 2<subtitle num="1" alias="subtitle alias 1">this is subtitle 1<text>this is a text<notice>this is a notice</notice></text></subtitle></title></regulation>'; # sort xml //echo $header."\n".utf8_encode($doc)."\n"; $doc = new DomTree($header.$doc); $root = $doc->getTag('regulation'); $tab = $doc->domxml2array($root[0]); print_r_html($tab); ?> ... the output should look like this easy looking array tree : Array ( [data] => [intitule#1] => Array ( [meta] => Array ( ) [data] => regulation 1 ) [title#2] => Array ( [meta] => Array ( [num] => 1 ) [data] => this is title 1 [tmp#3] => Array ( [meta] => Array ( [num] => 1 ) [data] => this is tmp ) ) [title#4] => Array ( [meta] => Array ( [num] => 2 ) [data] => this is title 2 [subtitle#5] => Array ( [meta] => Array ( [num] => 1 [alias] => subtitle alias 1 ) [data] => this is subtitle 1 [text#6] => Array ( [meta] => Array ( ) [data] => this is a text [notice#7] => Array ( [meta] => Array ( ) [data] => this is a notice ) ) ) ) ) ---- Server IP: 193.190.198.20 Probable Submitter: 88.147.20.202 ---- Manual Page -- http://www.php.net/manual/en/ref.domxml.php Edit -- https://master.php.net/note/edit/74285 Del: integrated -- https://master.php.net/note/delete/74285/integrated Del: useless -- https://master.php.net/note/delete/74285/useless Del: bad code -- https://master.php.net/note/delete/74285/bad+code Del: spam -- https://master.php.net/note/delete/74285/spam Del: non-english -- https://master.php.net/note/delete/74285/non-english Del: in docs -- https://master.php.net/note/delete/74285/in+docs Del: other reasons-- https://master.php.net/note/delete/74285 Reject -- https://master.php.net/note/reject/74285 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#125055) next »