Page 1 of 2
APPENDING to xml file using php
Posted: Fri Sep 17, 2004 6:15 pm
by Calipoop
I'm using flash, xml and php to create a guestbook-like feature. I want to be able to ADD new xml data to an existing document... not overwrite the previous entries...Here are the catches:
1 - I'd like to be able to add an xml node into the existing parent node, so that all entries fall under the same <MAIN></MAIN> tag
2 - I'd like to avoid using that php xml DOM stuff, mainly cause I don't understand it, and from what I've read, there's a chance that it won't be installed on the client's server I'll be posting to? (or maybe I just need clarification/reassurance if DOM is the only way to go?)
3 - this goes in hand with the append vs overwrite thing - if two users have the feature open at the same time, both can write to file without one of the entries being deleted!
Can anyone shed some light or point me in the right direction/other post?
Your help is much appreciated and admirable!
Posted: Fri Sep 17, 2004 6:25 pm
by Calipoop
oh yeah - this is the php I WAS using, but it doesn't seem to do the trick...
Code: Select all
<?php
$filename = "Stories.xml";
$raw_xml = file_get_contents("php://input");
print $raw_xml;
$fp = fopen($filename, "ab");
fwrite($fp, $raw_xml);
fclose($fp);
?>
Posted: Fri Sep 17, 2004 6:26 pm
by dethron
client's server
hmmm, i never heard this
following will be helpfull.
http://www.php.net/manual/en/ref.xml.php
Posted: Fri Sep 17, 2004 6:29 pm
by Calipoop
I know the specs of their server, just don't know them in detail, i.e. if DOM stuff is available...
They are APACHE 1.3.29 and PHP 4.3
Posted: Fri Sep 17, 2004 6:37 pm
by dethron
hmm, i see. you have some clients that is server.
it is a little bit compicated for a guestbook

(or -like feature)
and you do not have to know whether DOM stuff available or not, because DOM is not server side

Posted: Fri Sep 17, 2004 6:40 pm
by feyd
is there a reason you aren't using a database for this?
Posted: Fri Sep 17, 2004 6:49 pm
by Calipoop
thanks dethron for the clarification on DOM, that's good to know...
The reason I'm not using a database is that I'd like to have the option of reusing this feature wherever independent of what database system may or may not be installed. Plus I've been almost exclusive to actionscript thus far, and xml integration with actionscript is fairly advanced. I have strong knowledge of actionscript, and light knowledge of php.
Also, this feature will be temporary - about 2 months - it's for a movie website that will include user posts.
Thanks for your help thus far!
Also note - I'm doing all the xml structure formatting in flash, all I want is to append a node to the source xml
Posted: Fri Sep 17, 2004 6:52 pm
by dethron
your welcome.
once you become an expert on php, the actionscript will seem to you as a very simple tool

Re: APPENDING to xml file using php
Posted: Sat Sep 18, 2004 4:22 am
by timvw
Calipoop wrote:I'm using flash, xml and php to create a guestbook-like feature.
Meaby
http://www.amfphp.org/ can be usefull for you.
Calipoop wrote:
1 - I'd like to be able to add an xml node into the existing parent node, so that all entries fall under the same <MAIN></MAIN> tag
The principle is the same as editting a text or html. Don't see the catch?
Calipoop wrote:
2 - I'd like to avoid using that php xml DOM stuff, mainly cause I don't understand it, and from what I've read, there's a chance that it won't be installed on the client's server I'll be posting to? (or maybe I just need clarification/reassurance if DOM is the only way to go?)
You can use expat. Little sample i've written is at
http://home.mysth.be/~timvw/programming/php/service.txt
Calipoop wrote:
3 - this goes in hand with the append vs overwrite thing - if two users have the feature open at the same time, both can write to file without one of the entries being deleted!
[php_man]flock[/php_man]
Posted: Sat Sep 18, 2004 3:05 pm
by Weirdan
dethron wrote:hmm, i see. you have some clients that is server.
No, he has a client who ordered some system from him. That client has a server and want to serve some content to his own clients from that server.
dethron wrote:
it is a little bit compicated for a guestbook

(or -like feature)
Nothing complicated here.
dethron wrote:and you do not have to know whether DOM stuff available or not, because DOM is not server side

There's server side DOM as well: [php_man]dom[/php_man]
Moderatorial: Next time please make sure you know what you are talking about.
Posted: Mon Sep 20, 2004 1:49 pm
by Calipoop
thanks for the follow-ups fellas
Posted: Mon Sep 20, 2004 5:46 pm
by Calipoop
timvw - that sample you wrote looks like exactly what I need... thanks for the post.
In hopes of pushing my luck and avoiding additional research time, I was wondering if you could clarify a little? Say my xml file is
<STORIES>
<PERSON>
<NAME>Calipoop</NAME>
<TEXT>My story here</TEXT>
</PERSON>
</STORIES>
I want to open the STORIES tag and append another PERSON entry.
Do I need to fiddle with $flag = ' ' i.e. set it equal to STORIES? This is about as far as I've gotten. Assuming that's correct, will it auto append the $data that I've sent from flash? I also don't see any "write" functions in the script, I assume I need to add that?
Thanks so much for your help. If it's too much to discuss, just call me a lazy bastard and tell me to learn it for myself.
Posted: Tue Sep 21, 2004 4:12 am
by timvw
I'm not going to do your homework, only give hints
- The sample only does some basic parsing. It more or less converts the xml to a associative array.
- If you know how to handle arrays, [php_man]array[/php_man], you know how to add something to it at the right place.
- Writing in this case is fairly straightforwarded.
*) First you output <stories>
*) While you loop through the array, if the key value is not equal to stories, you need to output <key>value</key>.
*) Finally you output </stories>.
Posted: Tue Sep 21, 2004 3:54 pm
by Calipoop
here's a thought for comment...
instead of php parsing the entire document - I know where the appended data will need to go everytime: it will simply have to find "</STORIES>" at the end of the file.
Then I could just replace "</STORIES>" with:
<PERSON>
<NAME>Calipoop</NAME>
<TEXT>My story here</TEXT>
</PERSON>
</STORIES>
Does that make sense? Wouldn't that be faster? And if so, does php have a function to search a document? I'm finding this ereg_replace stuff, but it looks like that's just for strings?
Posted: Tue Sep 21, 2004 4:05 pm
by timvw
thus, if you read from a file (in this case xml).... it ends up in a variable. That variable happens to contain a "string" being the characters that where in the file.
Hurry, you can use string functions on that variable

Btw, if you read the file, line by line, you can use test for equality. $read == '</whatever>';