Instead of calling $xml->createElement try doing
$node = $xml->createNode(...);
$xml->append($node);
inside of your for loop.

On Thu, 2010-04-22 at 07:21 -0700, Jeremy Burns wrote:
> I am trying to produce an XML file ($results is an array of data):
> 
> <code>
>       App::import('Xml');
> 
>       $xml = new Xml();
> 
>       $xml->createElement('records', null, array(), 'namespace');
>       $xml->addNamespace('namespace', 'http://www.namespace.com');
>       $record = $xml->createElement('record', null, array('Id' => 1));
> 
>       foreach($results as $result):
>               $row = $xml->createElement('record', null, array('value' =>
> $result['value'], 'name' => $result['name']));
>       endforeach;
> 
>       echo $xml;
> </code>
> 
> The output is almost right, except each new node or element is always
> appended to the previous node, so I have way too many nests. I have
> tried setParent and append, but those commands are ignored.
> 
> Here's the output as it stands:
> 
> <namespace:records xmlns:namespace="http://www.namespace.com";>
>       <record id="1">
>               <record value="0" name="Some value">
>                       <record value="1" name="Some other value">
>                               <record value="0" name="Yet another value">
>                                       etc...
> 
> You might notice that the <record> nodes are not being closed off. I
> have tried all sorts of things to get this working, but to no avail.
> 
> Here's what I'm looking for:
> 
> <namespace:records xmlns:namespace="http://www.namespace.com";>
>       <record id="1">
>               <record value="0" name="Some value" />
>               <record value="1" name="Some other value" />
>               <record value="0" name="Yet another value" />
>               etc...
>       </record>
> </namespace:records>
> 
> In addition, how do I get the headers set (<?xml version="1.0"
> encoding="UTF-8"?>)?
> 
> Can anyone help please? This is driving me nuts.
> 
> Check out the new CakePHP Questions site http://cakeqs.org and help others 
> with their CakePHP related questions.
> 
> You received this message because you are subscribed to the Google Groups 
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com
> To unsubscribe from this group, send email to
> cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
> http://groups.google.com/group/cake-php?hl=en


Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to