Location: PHPKode > scripts > Xml2Array > test1.php
<?php
/**
 * test1.php
 * $Header: d:/cvs/classistd/x2a/test1.php,v 1.5 2004/02/10 21:51:06 Administrator Exp $
 * Test module for CXml2Array
 *
 */
require 'CXml2Array.php';


$InName='test.xml';
if(!$TplDom = domxml_open_file(realpath($InName)))
   trigger_error('Error parsing the file ',E_USER_ERROR);

$root = $TplDom->document_element();
$x=new CXml2Array($root);
echo '<hr>';
echo 'Dump the array<br>';
$x->ArrayDump();
// die();
echo '<hr>';
// test the retrieve functions
echo 'Exist tag pippo ? '.(($x->ExistTag('pippo'))?'Y':'N').'<br>';
echo 'Exist tag menul ? '.(($x->ExistTag('menul'))?'Y':'N').'<br>';
// Must load the element voc, it has some attribute
// voce is child of menul
// menul is child of root element

$menul=new CXml2Array($x->GetTagPos('menul',0));
$voce=new CXml2Array($menul->GetTagPos('voce')); // defualt return child 0
echo 'Exist attribute pippo ? '.(($voce->ExistAttribute('pippo'))?'Y':'N').'<br>';
echo 'Exist attribute desc ? '.(($voce->ExistAttribute('desc'))?'Y':'N').'<br>';
echo 'Attribute desc value '.$voce->GetAttribute('desc').'<br>';

echo '<hr>';
echo 'Child title text: ';
$title=new CXml2Array($x->GetTagPos('title')); // defualt return child 0
echo $title->GetText();

echo '<hr>';
echo 'Iterate over all attributes<br>';
while(($ret=$voce->EachAttribute())!=FALSE)
{
   list($key,$value)=$ret;
   echo $key.'='.$value.'<br>';
}

echo '<hr>';
echo "Iterate over all node's child (child's value as array)<br>";
while(($ret=$menul->EachChildArray())!=FALSE)
{
   list($key,$value)=$ret;
   echo '<br>...key: '.$key.'<br>';
   var_dump($value);
}
echo "It shows the 4 childs of 'menul' called 'voce'<br>";

echo '<hr>';
echo "Iterate over all node's child (child's value as object CXml2Array)<br>";
$menul->EachChild(TRUE);   // reset the array pointer
while(($ret=$menul->EachChild())!=FALSE)
{
list($key,$value)=$ret;
echo '<br>...key: '.$key.'<br>';
var_dump($value);
}
echo "It shows the 4 childs of 'menul' called 'voce'<br>";

echo '<hr>';
echo 'Dump the \'voce\' array<br>';
$voce->ArrayDump();

echo '<hr>';
echo 'The full array<br>';
var_dump($x->GetArray());

echo '<hr>';
echo 'GetTag(\'menul\')<br>';
var_dump($x->GetTag('menul'));

echo '<hr>';
echo 'GetTagChilds(\'menul\')<br>';
var_dump($x->GetTagChilds('menul'));

echo '<hr>';
echo 'GetTagAttributes(\'menul\')<br>';
var_dump($x->GetTagAttributes('menul'));


?>
Return current item: Xml2Array