After reading Chris Hartjes post in my previous thread, I thought I'd
take his empty xml component and add some code.  Below is what I came
up with and I'm wondering what you guys think and whether something
like this fits within the cake paradigm.

<?php

  class XMLComponent extends Object {

    var $layout = 'xml';
    var $enabled = true;

    // This is so you can specify which nodes you do not want
    // included in the XML.
    var $excludedNodes = array();

    var $version = '1.0';
    var $encoding = 'UTF-8';
    var $rowNameNode = '';

    var $__domDocument = NULL;

    function startup(&$controller) {
      if (!$this->enabled || !( strtolower(
$controller->params['webservices'] ) == strtolower( 'xml' ))) {
        return true;

      }
      $this->__domDocument = new DOMDocument( $this->version,
$this->encoding );

    }

    function buildXMLBody( $nodeName, $nodeValue ) {
      $retval = NULL;

      /**
       * Using the 3rd argument because evidently if the needle
argument
       * is a numeric 0, it will find it in the array somehow.  Not
exactly sure
       * why that's happening...
       **/
      if( !( in_array( $nodeName, $this->excludedNodes, TRUE ))) {

        if( is_numeric( $nodeName )) {
          // If I don't do this, the DOM throws an exception.
          // Evidently, it doesn't like numbers as node names
          $nodeName = $this->rowNameNode;

        }
        if( is_array( $nodeValue )) {
          $domElement = $this->__domDocument->createElement( $nodeName
);
          foreach( $nodeValue as $name => $value ) {
            if( $node = $this->buildXMLBody( $name, $value )) {
              $domElement->appendChild( $node );

            }
          }
        } else {
          $domElement = $this->__domDocument->createElement( $nodeName
);
          $domElement->appendChild(
$this->__domDocument->createTextNode( htmlentities( $nodeValue )));

        }
        $retval = $domElement;

      }
      return $retval;

    }

    function dataToXML( $dataArray, $rootNodeName = 'root',
$rowNameNode = 'row' ) {

      $retval = NULL;
      $this->rowNameNode = $rowNameNode;

      $rootNode = $this->__domDocument->createElement( $rootNodeName );

      foreach( $dataArray as $nodeName => $nodeValue ) {
        if( $domElement = $this->buildXMLBody( $nodeName, $nodeValue ))
{
          $rootNode->appendChild( $domElement );

        }
      }
      $this->__domDocument->appendChild( $rootNode );

      $retval = $this->__domDocument->saveXML( NULL, LIBXML_NOEMPTYTAG
);

      return $retval;

    }
  }

?>


and here is my controller:

class UsersController extends AppController {

  var $name = 'Users';

  /**
   * I don't need to include Xml here.  It's being including
   * by something else within CakePHP.
   * #shrug#
   * Is that right?  Should it be doing that?  I'm not including
   * it in the $components array in my app_controller.php
   * file...
   **/
  var $components = array();

  function index() {

    $this->User->recursive = -1;

    if( strtolower( $this->params['webservices'] ) == strtolower( 'xml'
)) {
      $this->Xml->excludedNodes = array_merge(
$this->Xml->excludedNodes, array( 'id', 'password', 'role', 'email' ));
      $this->set('users', $this->Xml->dataToXML(
$this->User->findAll(), 'users' ));

    } else {
      // Snip
      // Do regular index-y stuff here
    }
  }
}

What do you guys think?  Am I going about this all the wrong way?  I
figured that putting this code in the component would allow any of the
other controllers that want to use the XML webservice a mechanism to
transform it's data into XML.  Should this functionality be moved to
the helper instead?  Is that were it's better suited?

thnx,
Christoph


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Cake PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to