APPENDING to xml file using php
Moderator: General Moderators
APPENDING to xml file using php
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!
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!
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);
?>hmmm, i never heard thisclient's server
following will be helpfull.
http://www.php.net/manual/en/ref.xml.php
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
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
Re: APPENDING to xml file using php
Meaby http://www.amfphp.org/ can be usefull for you.Calipoop wrote:I'm using flash, xml and php to create a guestbook-like feature.
The principle is the same as editting a text or html. Don't see the catch?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
You can use expat. Little sample i've written is at http://home.mysth.be/~timvw/programming/php/service.txtCalipoop 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?)
[php_man]flock[/php_man]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!
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:hmm, i see. you have some clients that is server.
Nothing complicated here.dethron wrote: it is a little bit compicated for a guestbook(or -like feature)
There's server side DOM as well: [php_man]dom[/php_man]dethron wrote:and you do not have to know whether DOM stuff available or not, because DOM is not server side
Moderatorial: Next time please make sure you know what you are talking about.
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.
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.
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>.
- 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>.
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?
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?
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>';
Hurry, you can use string functions on that variable