Page 1 of 1

Printing XML String

Posted: Sun Nov 27, 2005 6:55 am
by michel77
How to print an XML String ? Simply print the String doesn't work. Loading with simplexml_load_string and using print_r shows only the structure, not the data. I would like to print the whole String exactly. How to do it ?

Re: Printing XML String

Posted: Sun Nov 27, 2005 7:06 am
by foobar
You mean, just print out the textual contents?

Code: Select all

$fp = fopen('/path/to/file.xml', 'r');

while ($tmp = fread($fp, 4096)) {
  $file .= $tmp;
}

echo $file;
^Works for any file.

Posted: Sun Nov 27, 2005 7:17 am
by michel77
Here an axample:

$xml = '<?xml version="1.0" encoding="UTF-8"?>';
print ($xml);

this doesn't work

Posted: Sun Nov 27, 2005 7:26 am
by foobar
michel77 wrote:Here an axample:

$xml = '<?xml version="1.0" encoding="UTF-8"?>';
print ($xml);

this doesn't work
Yes it does. You just won't see it in your browser. Check the output's source code.

Posted: Sun Nov 27, 2005 7:40 am
by michel77
You are right. But not only the browser also Eclipse (PHP Browser) doesn't show it. Do you know how the access the output within eclipse or how to redirect into a file ?

Posted: Sun Nov 27, 2005 7:46 am
by foobar
michel77 wrote:You are right. But not only the browser also Eclipse (PHP Browser) doesn't show it. Do you know how the access the output within eclipse or how to redirect into a file ?
Not sure what you want to do, but I assume you want to save the xml output as a file?

Posted: Sun Nov 27, 2005 7:56 am
by michel77
First I would like to get teh output's source code within Eclipse. I know only the PHP browser, that shows the same like Internet Explorer. Addutionally I would like to redirect outpout (print) into a file.

Posted: Sun Nov 27, 2005 7:59 am
by foobar
michel77 wrote:First I would like to get teh output's source code within Eclipse. I know only the PHP browser, that shows the same like Internet Explorer. Addutionally I would like to redirect outpout (print) into a file.
Hm...ok.

Code: Select all

$fp = fopen('/path/to/file.xml', 'r');

while ($tmp = fread($fp, 4096)) {
  $file .= $tmp;
}

fclose($fp2);

/* Write to file */
$fp2 = fopen('/some/other/file.xml', 'w');
fwrite($file, $file);
fclose($fp2);

/* Replaces < and > with < and > */
$file = str_replace('<', '<', $file);
$file = str_replace('>', '>', $file);

/* Output the contents */
echo $file;