APPENDING to xml file using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Calipoop
Forum Newbie
Posts: 24
Joined: Fri Sep 17, 2004 6:06 pm
Location: Los Angeles, CA

APPENDING to xml file using php

Post 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!
Calipoop
Forum Newbie
Posts: 24
Joined: Fri Sep 17, 2004 6:06 pm
Location: Los Angeles, CA

Post 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);
?>
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

client's server
hmmm, i never heard this :(


following will be helpfull.
http://www.php.net/manual/en/ref.xml.php
Calipoop
Forum Newbie
Posts: 24
Joined: Fri Sep 17, 2004 6:06 pm
Location: Los Angeles, CA

Post 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
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is there a reason you aren't using a database for this?
Calipoop
Forum Newbie
Posts: 24
Joined: Fri Sep 17, 2004 6:06 pm
Location: Los Angeles, CA

Post 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
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

your welcome.
once you become an expert on php, the actionscript will seem to you as a very simple tool :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: APPENDING to xml file using php

Post 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]
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
Calipoop
Forum Newbie
Posts: 24
Joined: Fri Sep 17, 2004 6:06 pm
Location: Los Angeles, CA

Post by Calipoop »

thanks for the follow-ups fellas
Calipoop
Forum Newbie
Posts: 24
Joined: Fri Sep 17, 2004 6:06 pm
Location: Los Angeles, CA

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I'm not going to do your homework, only give hints :D

- 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>.
Calipoop
Forum Newbie
Posts: 24
Joined: Fri Sep 17, 2004 6:06 pm
Location: Los Angeles, CA

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 :P Btw, if you read the file, line by line, you can use test for equality. $read == '</whatever>';
Post Reply