XML_Tree extra features...

From: Date: Thu, 07 Aug 2003 05:07:14 +0000
Subject: XML_Tree extra features...
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-19366@lists.php.net to get a copy of this message
attached should be a patch to add a number of features to XML_Tree. A) replacement nodes / callback processing. This allows you to assign a callback to any XML tag, and call a function to return a replacement node. <transactions> <transaction> <member>A-123</member> <value>12.34</value> <date>27/07/03</date> </transaction> ... </transactions> $tree = &new XML_Tree('somefile.xml','1.0', array( 'mapping' => array( // call MyClass::parseTransaction on the transaction tag 'transaction' => array('MyClass', 'parseTransaction'), 'transactions' => array('MyClass', 'parseTransactions') ))); $root = &$tree->getTreeFromFile (); echo "<PRE>";print_r($root); the two methods.. function parseTransaction(&$node) { $ret = new StdClass; foreach($node->children as $n) { $ret->{$n->name} = $n->content; } return $ret; } function parseTransactions(&$node) { return $node->children; } Array ( [0] => stdClass Object ( [date] => 27/07/03 [member ] => A-123 [value] => 12.34 ) ..... Obviously you can return null, from the method, and process as you go along... B) buildNodes/search/ This assigns parent and id's to all nodes, index/aliases them in a nodes[] array, and enable you to do.. $tree = &new XML_Tree('somefile.xml','1.0', array( 'buildNodes' => true) ); $node = &$tree->search(0, array('member',array('id'=>123))); look for <member id="123"> node $node = &$tree->search(0, array('member',array(),'A-123')); look for <member>A-123</member> $node = &$tree->searchPath(0, array('transaction','transaction', array('member',array(),'A-123') ) ); looks for <transactions> <transaction> <member>A-123</member> $parent = &$tree->parent($node); retreives the parent node.. Let me know your thoughts.. Regards Alan -- Can you help out? Need Consulting Services or Know of a Job? http://www.akbkhome.com

« previous php.pear.dev (#19366) next »