Serializing find("threaded") data to XML

This article is also available in the following languages:
By rreyes
Ever tried to serialize a full find("threaded") tree to XML? Well, it won't be easy to consume. Unless you give a try with XMLTreeHelper.

Usage


Controller Class:

<?php 
class NodesController extends AppController {
    
    var 
$name 'Nodes';
    var 
$components = array('RequestHandler');
    var 
$helpers = array("Xmltree");
    
    function 
index() {
        
$data $this->Node->find("threaded", array("order" => "lft""contain" => false));
        
$this->set("objects"$data);
    }
}
?>

View Template:


<data>
    <?php 
        
echo $xmltree->serialize($objects"Node"); 
    
?>
</data>

Code

Helper Class:

<?php 
    
class XmltreeHelper extends AppHelper {
        var 
$helpers = array('Xml');
         
        function 
serialize($treeData$containerName NULL){
            
debug($treeData);
            
$this->normalize($treeData$containerName);
            return 
$this->Xml->serialize($treeData);
        }
         
        function 
normalize(&$children$containerName){
            if(
sizeof($children) > 0){
                foreach(
$children as &$node){
                    
$this->normalize($node["children"], $containerName);
    
                    if(
sizeof($node["children"]) > 0){
                        
$node[$containerName][$containerName] = array();
                            
                        foreach(
$node["children"] as &$child){
                            
$node[$containerName][$containerName][] = $child[$containerName];
                        }
                    }
    
                    unset(
$node["children"]);
                }
            }
        }
         
    }
?>

Comments

  • Posted 10/09/09 09:31:12 AM
    I have modified this helper to filter the fields that gets printed. Even though the fields can be limited through the model with "fields", some fields are necessary to get the tree structure (ie. lft, rght, parent_id, id) and you might not want them in the XML so with the modification below you can limit the fields to be printed.

    Helper Class:

    <?php 
    <?php 
        
    class XmltreeHelper extends AppHelper {
            var 
    $helpers = array('Xml');
            var 
    $fields=null;
             
            function 
    serialize($treeData$containerName NULL,$fields=null){
                
    $this->fields=$fields;
                
    $this->normalize($treeData$containerName);
                return 
    $this->Xml->serialize($treeData);
            }
             
            function 
    normalize(&$children$containerName){
                if(
    sizeof($children) > 0){
                    foreach(
    $children as &$node){
                        
    $this->normalize($node["children"], $containerName);
        
                        if(
    sizeof($node["children"]) > 0){
                            
    $node[$containerName][$containerName] = array();
                                
                            foreach(
    $node["children"] as &$child){
                                
    $node[$containerName][$containerName][] = $child[$containerName];
                            }
                        }
        
                        unset(
    $node["children"]);
                        if(!
    is_null($this->fields)){
                            foreach(
    $node[$containerName] as $field=>$value){
                                if(!
    in_array($field,$this->fields) && $field!=$containerName){
                                    unset(
    $node[$containerName][$field]);
                                }
                            }
                        }
                    }
                }
            }
             
        }
    ?>
    ?>
  • Posted 03/11/09 07:40:53 AM
    Could you perhaps post an example of output?

Comments are closed for articles over a year old