Help Writing XML with SimpleXML

Greetings,

I am trying to write XML using SimpleXML for a web service call. I was having success until the XML got a little more complicated. Here is the XML format where I am running into problems:


<Agent>
    <Person Last='Smith' First='John'>
        <Addresses />
        <PhoneNumbers>
            <Phone Type='1' Number='888-555-1212'>
        </PhoneNumbers>
    </Person>
</Agent>

Here is my php:


$xmlOutput = new SimpleXMLElement('<?xml version="1.0"?><ReportRequest> </ReportRequest>');
$xmlOutput->addAttribute('CID','9200');
$xmlOutput->addAttribute('Diagram','1');
$xmlOutput->addAttribute('DueDate','2011-11-15');
$xmlOutput->addAttribute('NumPhotos','6');
$xmlOutput->addAttribute('InspectAfter','');
$xmlOutput->addAttribute('PolicyNumber','JTC0004425');
$reportType = $xmlOutput->addChild('ReportType');
$reportType->addAttribute('CPType','Commercial');
$reportType->addAttribute('SectionIDs','');
$reportType->addAttribute('Description','');
$reportType->addAttribute('ReportTypeID','123');
$locations =  $xmlOutput->addChild('Locations')->addChild('Addresses')->addChild('Address');
$locations->addAttribute('Zip','91216');
$locations->addAttribute('City','Chatsworth');
$locations->addAttribute('Line1','123 Main St');
$locations->addAttribute('Line2','Suite 201');
$locations->addAttribute('State','CA');
$locations->addAttribute('Latitude','');
$locations->addAttribute('Longitude','');
$agent =  $xmlOutput->addChild('Agent')->addChild('Person')->addChild('Addresses')->addChild('PhoneNumbers')->addChild('Phone');
$agent->addAttribute('Last','Smith');
$agent->addAttribute('Email','');
$agent->addAttribute('First','John');
$agent->addAttribute('Title','');
$agent->addAttribute('Type','1');
$agent->addAttribute('Number','888-555-1212');
$agent->addAttribute('TypeName','Office');
$agent->addAttribute('Extension','');

Header('Content-type: text/xml');

echo $xmlOutput->asXML();

Everything outputs as it should until the line that starts with $agent. I want the output to match the section above. I have tried several variations but I cannot seem to figure it out. I know the current PHP sample doesn’t work. Help??

Thanks in advance,

John

How about:


$agent =  $xmlOutput->addChild('Agent');
$person =  $agent->addChild('Person');
$person->addAttribute('Last','Smith');
$person->addAttribute('First','John');
$addresses =  $person->addChild('Addresses');
$phonenumbers = $person->addChild('PhoneNumbers');
$phone = $phonenumbers->addChild('Phone');
$phone->addAttribute('Type','1');
$phone->addAttribute('Number','888-555-1212');

Thanks Guido, that was it. Stupid mistake.

thanks again.

j