Here I have adjusted NCM's code as it did not use the File class
and was outputting the generated xml to the file twice (one after the other)
also I have amended to have a id which is Stephen22
$xmlData = array(
'person' => array(
'id' => 'Stephen22',
'name' => 'Stephen',
'age' => 22,
'hair' => array(
'length' => 'medium',
'colour' => 'dark brown'
)
)
);
App::import('Helper', 'Xml');
App::import('Core', 'File');
$xml = new XmlHelper();
$file = $xml->header(); // We need the XML header before our data.
$file .= $xml->serialize($xmlData, array('whitespace' => true));
// Write the file.
$xmlFile = new File(TMP . '/filename.xml');
$xmlFile->write($file, 'w');
$xmlFile->close();
========
<?xml version="1.0" encoding="UTF-8" ?>
<person id="Stephen22" name="Stephen" age="22">
<hair length="medium" colour="dark brown" />
</person>
------------
Whereas
$xmlData = array(
'person' => array(
'id' => 'Stephen22',
'name' => 'Stephen',
'age' => 22,
'hair' => array(
'length' => 'medium',
'colour' => 'dark brown'
),
'being' => array('I am a fish')
)
);
outputs
<?xml version="1.0" encoding="UTF-8" ?>
<person id="Stephen22" name="Stephen" age="22">
<hair length="medium" colour="dark brown" />
<being>
<![CDATA[I am a fish]]>
</being>
</person>
- S
On 18 February 2011 15:19, Matthew McNaughton <[email protected]>wrote:
> Greetings all,
> I was wondering if someone could point me to a more detailed description of
> the XML Helper and how to produce a desired xml file from a php array.
>
> I've gone through the following documentation
> http://book.cakephp.org/view/1485/Xml
> http://book.cakephp.org/view/1473/XML
> http://book.cakephp.org/view/1238/REST
>
> Even found this external link, which I thought to be the most useful
>
> http://www.ninjacodermonkey.co.uk/2010/11/cakephp-array-to-xml-from-within-controller/
>
> Essentially, what I'd like to know some more about is whether the xml
> helper allows you to do things like add tag ids. Also, if there were any
> more resources out there that others had come accross on how the php arrays
> should be structured etc.
>
> Let me know.
>
> Regards,
> Mmm
> --
> Our newest site for the community: CakePHP Video Tutorials
> http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help
> others with their CakePHP related questions.
>
>
> To unsubscribe from this group, send email to
> [email protected] For more options, visit this group
> at http://groups.google.com/group/cake-php
>
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php