0

well what i wnat to do is create a xml file and download it, if it is possible not saving in the server.

what i have so far is creating the xml with DOMDocument and store it in the server

<?php
$domTree = new DOMDocument('1.0', 'UTF-8');
$rootXML = $domTree->createElement( 'XML' );
$rootXML = $domTree->appendChild($rootXML);

$personalData = $domTree->createElement( 'PERSONAL' );
$personalData = $rootXML->appendChild($personalData);
$personalData ->nodeValue = "Alan";
$domTree->save('MyXmlFile.xml');
?>

i know i supposed to use something like this

<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');

echo $xml_contents;
?>

but using DOMDocument im lost in hwo using it, THANKS IN ADVANCE!

1 Answers1

0

To output to the standard output - that is what PHP sends to the browser - you can make use of the PHP output stream:

header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');
$domTree->save('php://stdout');

This principle work with (nearly) anything in PHP that expects a filename which is a pretty nice feature. Compare for example with my answer to phpexcel to download.

It is merely the same as when you would do:

echo $domTree->saveXML();

as echo as well "prints" to the standard output. But for larger data, the concept of a stream is preferable.

If you want to learn more about standard output, Wikipedia has an overview about these so called standard streams:

For the correct XML content-type, this depends a bit what you do, I suggest taking a look into if you like to learn a bit more:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836