Jump to content

Help Writing XML with SimpleXML


johnnc

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/251632-help-writing-xml-with-simplexml/
Share on other sites

You have to add attributes to the specific block. Like this:

 

<?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');
$agent = $agent->addChild('Person');
$agent->addAttribute('Last','Smith');
$agent->addAttribute('First','John');
$agent->addAttribute('Email','');
$agent->addAttribute('Title','');
$agent->addAttribute('Type','1');
$agent = $agent->addChild('Addresses');
$agent = $agent->addChild('PhoneNumbers');
$agent = $agent->addChild('Phone');
$agent->addAttribute('Number','888-555-1212');
$agent->addAttribute('TypeName','Office');
$agent->addAttribute('Extension','');

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

echo $xmlOutput->asXML();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.