2013-01-08 11 views
5

chcę utworzyć XML w następującym formacieCakePHP generowania XML

<?xml version="1.0" encoding="utf-8"?> 
<categories> 
    <category> 
     <name>Hill Station</name> 
     <slug>hill_station</slug> 
    </category> 

    <category> 
     <name>Water Fall</name> 
     <slug>water_fall</slug> 
    </category> 

    <category> 
     <name>Wild Life</name> 
     <slug>wild_life</slug> 
    </category> 

    <category> 
     <name>Piligrim</name> 
     <slug>piligrim</slug> 
    </category> 

    <category> 
     <name>Archeology</name> 
     <slug>archeology</slug> 
    </category> 
</categories> 

Mam tablicy w następujący sposób tworzyć bazy danych, które chcę tłumaczyć z powyższym formacie.

array(
    (int) 0 => array(
     'Category' => array(
      'id' => '4', 
      'name' => 'Archeology', 
      'slug' => 'archeology', 
      'is_active' => true, 
      'created' => '2013-01-08 07:34:07', 
      'modified' => '2013-01-08 07:34:07' 
     ) 
    ), 
    (int) 1 => array(
     'Category' => array(
      'id' => '1', 
      'name' => 'Hill Stations', 
      'slug' => 'hill-stations', 
      'is_active' => true, 
      'created' => '2013-01-08 07:33:39', 
      'modified' => '2013-01-08 07:33:39' 
     ) 
    ), 
    (int) 2 => array(
     'Category' => array(
      'id' => '3', 
      'name' => 'Waterfall', 
      'slug' => 'waterfall', 
      'is_active' => true, 
      'created' => '2013-01-08 07:33:54', 
      'modified' => '2013-01-08 07:33:54' 
     ) 
    ), 
    (int) 3 => array(
     'Category' => array(
      'id' => '2', 
      'name' => 'Wild Life', 
      'slug' => 'wild-life', 
      'is_active' => true, 
      'created' => '2013-01-08 07:33:47', 
      'modified' => '2013-01-08 07:33:47' 
     ) 
    ) 
) 

co starałem

$this->Category->recursive = -1; 
$categories = $this->Category->find('all', array('order' => 'Category.name asc')); 
debug($categories); 

$category = array(); 
foreach($categories as $c){ 
    $cat['name'] = $c['Category']['name']; 
    $cat['slug'] = $c['Category']['slug']; 
    $cat['active'] = ($c['Category']['is_active'] == 1) ? true : false; 


    $category['categories']['category'][] = $cat; 
} 
//debug($category); 

$xml = Xml::build($category); 

echo $xml; 
debug($xml); 

nie jestem w stanie wygenerować plik XML za pośrednictwem powyższego kodu.

Odpowiedz

5
$xmlObject = Xml::fromArray($category); 
$xmlString = $xmlObject->asXML();