note 74285 rejected from ref.domxml by bjori
| From: | bjori@php.net | Date: | Tue, 17 Apr 2007 09:22:02 +0000 |
| Subject: | note 74285 rejected from ref.domxml by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125492@lists.php.net to get a copy of this message | ||
Note Submitter: jl_beuzard at hotmail dot com
----
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
)
)
)
)
)