Page 1 of 1

problem with file(), fgets(), ..returning incomplete file

Posted: Sat Mar 11, 2006 7:48 pm
by sachincrp
I'm dynamically creating an xml document in my php script with values from a
user form.


$xmlFile = "myxml.xml";

$fileHandle = fopen($xmlFile, 'wb') or die ('Could not open file!');

$xmlData = '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
.
.
.
.
fclose($fileHandle);

all this works..a valid xml document is created just like i want


But my problem is reading it. I want to be able to insert a new element into the xml document

So this is what I do rite now:

readfile($fileName);

when i do this, it does not print the entire contents of the xml file.
It only prints the values of the elements. It skips the actual elemtns.

for example: if part of my xml is something like this

<student>
<name>John</name>
<address>123 street</address>
</student>

my code only prints
John 123 street

it skips the xml tags. I dont understand why. I've tried the other ways to read files too such as
fgets, fread, file() and it does the same. Only values of the elemtns are printed.

Someone please help me with this. Why aren't my xml tags getting printed.

Another thing i also tried was instead of making this an .xml file, i made it a simple .txt
and tried the same thing, but same result.

PLEASE help

Posted: Sat Mar 11, 2006 7:54 pm
by feyd
Your script sounds like it is sending a content-type of for HTML, the default content-type for PHP to send.

Try calling header('Content-type: text/xml') before you output anything.

still not working

Posted: Sun Mar 12, 2006 12:50 pm
by sachincrp
when i tried header('Content-type: text/xml'); before i printed anything, i got the following warning:

Warning: Cannot modify header information - headers already sent by (output started at /www/cstv/html/admin/videoproject/test.php:10) in /www/cstv/html/admin/videoproject/test.php on line 274

but any how, the xml that i want to see did get printed in the page source. so the file is returning it..

but i want to be able to insert an element to the xml file, and then write it back using fwrite.

So what's the best way to do this?

Re: still not working

Posted: Sun Mar 12, 2006 12:58 pm
by feyd
sachincrp wrote:Warning: Cannot modify header information - headers already sent by (output started at /www/cstv/html/admin/videoproject/test.php:10) in /www/cstv/html/admin/videoproject/test.php on line 274
54 viewtopic.php?t=1157
sachincrp wrote:but any how, the xml that i want to see did get printed in the page source. so the file is returning it..

but i want to be able to insert an element to the xml file, and then write it back using fwrite.

So what's the best way to do this?
Depending on your server's installed components, you could parse the XML inserting tags as needed, or use regex, or strpos()-substr_replace().. there are xml parsing tools on pear.php.net too: http://pear.php.net/search.php?q=xml&in=packages