tags:

views:

90

answers:

3

I need sample code for a PHP function that returns XML.

+5  A: 
function printxml() {
  echo "<xml></xml>";
}
Col. Shrapnel
Or even just `<xml />`.
Gumbo
this doesn't return xml
Tom Haigh
+5  A: 
function foo()
{
    return '<root><answer>Not doing your homework for you.</answer></root>';
}
Coronatus
typo! ---------
Pekka
Redrum⁠⁠⁠⁠⁠⁠⁠⁠⁠
Gumbo
+1  A: 

From PHP manual on DOM

$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;

$root = $doc->createElement('book');
$root = $doc->appendChild($root);

$title = $doc->createElement('title');
$title = $root->appendChild($title);

$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);

echo "Saving all the document:\n";
echo $doc->saveXML() . "\n";

echo "Saving only the title part:\n";
echo $doc->saveXML($title);
Gordon