Create an xml document within PHP

I need to create xml documents on the fly with data coming from an sql statement ( the date will be going into a Flex application ). I am having trouble getting the xmlwiter functions to write the document. I am using xmlwriter_open_memory to get my resource. Here's my code ( this is just test code, I will add the dynamic data after it works).

 

$xml_resource = xmlwriter_open_memory();

if ($xml_resource):
xmlwriter_start_document($xml_resource);
xmlwriter_start_attribute($xml_resource , 'access_year');
xmlwriter_write_attribute($xml_resource, 'access_year' , '2008');
xmlwriter_end_attribute($xml_resource);

xmlwriter_start_element($xml_resource , 'appArea');
xmlwriter_write_element($xml_resource , 'appArea' , 'Family_Contacts');
xmlwriter_end_element($xml_resource);

xmlwriter_end_document($xml_resource);

$xml_file = xmlwriter_output_memory($xml_resource, false);

echo $xml_file;
endif;

 

 

I get Family_Contacts as the only output. Where did I go wrong?

Thanks for any help.

Lise