2

Is it possible to use the php://temp wrapper to generate an XML file with XMLWriter? I like the features it provides (memory for small files, transparent temporary file for larger output) but I can't get the syntax (if it's even possible):

<?php

header('Content-type: text/xml; charset=UTF-8');

$oXMLWriter = new XMLWriter;
$oXMLWriter->openURI('php://temp');
$oXMLWriter->startDocument('1.0', 'UTF-8');

$oXMLWriter->startElement('test');
$oXMLWriter->text('Hello, World!');
$oXMLWriter->endElement();

$oXMLWriter->endDocument();
// And now? *******
$oXMLWriter->flush();

2 Answers 2

1

I don't understand the purpose of writing to a temp file. Perhaps you want:

$oXMLWriter->openURI('php://output');

I haven't ever used XMLWriter but it doesn't seem to take a handle to a file pointer. I think that's really what you want.

For giggles, here's something that wraps the temp interface:

class WeirdStream
{
  static public $files = array();
  private $fp;

  public function stream_open($path)
  {
    $url = parse_url($path);
    self::$files[$url['host']] = fopen('php://temp', 'rw');
    $this->fp = &self::$files[$url['host']];
    return true;
  }

  public function stream_write($data)
  {
    return fwrite($this->fp, $data);
  }
}

stream_wrapper_register('weird', 'WeirdStream');

$oXMLWriter = new XMLWriter;
$oXMLWriter->openURI('weird://a');
// .. do stuff
$oXMLWriter->flush();

Now you can get at the file pointer:

$fp = WeirdStream::$files['a'];

It may be purely in memory, or it may be a temporary file on disk.

You could then loop through the data line by line:

fseek($fp, 0, SEEK_SET);
while (!feof($fp)) $line = fgets($fp);

But this is all very odd to me.

Sign up to request clarification or add additional context in comments.

6 Comments

Purpose? Store it in a database or add it to a ZIP archive.
I saw the content-type, so I assumed you were displaying it to the screen. Are you doing both stdout and saving in a DB, etc?
My code was just a simplified example to work on the technique :)
To me, the final destination is a crucial piece of information when it comes to deciding which interface to use... XMLWriter doesn't seem to take a pointer to an existing stream, so I don't think you'll get your dual memory/file interface working unless you perhaps register your own custom stream wrapper that does that.
is it possible to output buffer (ob_start etc.) that stream?
|
0

What do you need to do with the contents of php://temp eventually? If you just need a temporary, memory-only storage, then you can use openMemory():

$oXMLWriter = new XMLWriter;
$oXMLWriter->openMemory();
$oXMLWriter->startDocument('1.0', 'UTF-8');

$oXMLWriter->startElement('test');
$oXMLWriter->text('Hello, World!');
$oXMLWriter->endElement();

$oXMLWriter->endDocument();
echo $oXMLWriter->outputMemory ();

3 Comments

Perhaps he doesn't want to store it in memory if it exceeds a certain threshold. It's a bit strange though, because it seems like you'd know before you start how big the file is going to be, at least to some reasonable approximation.
If he wants to store it in a DB (as suggested above), then I think he must at some point have it in memory. As you said, without knowing the final destination, difficult to provide a good help.
Yes, it seems like for most things you'd either always want it entirely in memory (to place in a db) or always saved directly to disk (to store) or always sent directly to stdout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.