note 74269 added to ref.domxml
| From: | jl_beuzard at hotmail dot com | Date: | Tue, 03 Apr 2007 02:15:12 +0000 |
| Subject: | note 74269 added to ref.domxml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125023@lists.php.net to get a copy of this message | ||
This short recursive fill in a simple array 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;
}
}
// >((dom)node,&tab,&i) ((array)tab)>
function xml2array($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->xml2array($next,$tab[$next->node_name()."#".$i],$i);
break;
}
}while($next=$next->next_sibling());
}
return $tab;
}
}
$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>';
$doc = new DomTree($header.$doc);
$root = $doc->getTag('regulation');
$tab = $doc->xml2array($root[0]);
print_r_html($tab);
?>
... should return an easy looking tree like :
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/74269
Del: integrated -- https://master.php.net/note/delete/74269/integrated
Del: useless -- https://master.php.net/note/delete/74269/useless
Del: bad code -- https://master.php.net/note/delete/74269/bad+code
Del: spam -- https://master.php.net/note/delete/74269/spam
Del: non-english -- https://master.php.net/note/delete/74269/non-english
Del: in docs -- https://master.php.net/note/delete/74269/in+docs
Del: other reasons-- https://master.php.net/note/delete/74269
Reject -- https://master.php.net/note/reject/74269
Search -- https://master.php.net/manage/user-notes.php