Now that we have the data we need to sort out the response. We'll use the
JSimpleXMLElement class to build the XML response:
// import library
jimport('joomla.utilities.simplexml');
// create root node
$xml = new JSimpleXMLElement('item', array('id' => $data->id));
This creates a root node of type item with an attribute id populated with the value
of the chosen item's ID. Now we can add some sub-nodes:
// add children
$name =& $xml->addChild('name');
$text =& $xml->addChild('text');
// set child data values
$name->setData($data->name);
$text->setData($data->text);
APIs and Web Services
[ 286 ]
This adds two sub-nodes, name and text, and populates them with the item's
corresponding values.
Now that we have built our XML response, our last task is to output the XML. We
start with the XML declaration and then use the toString() method:
echo ''."\n";
echo $xml->toString();
If we were to test this, we would experience a slight oddity; the response will be
displayed as plain text.
Pages:
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392