PDA

View Full Version : php to xml


THOR
09-06-2002, 02:10 AM
i have a flash file to output xml formatted text to a php file, which works fine

i have an xml file that has this text

<?xml version="1.0" encoding="iso-8859-1" ?><!DOCTYPE chat>
<chat></chat>

giving me a chat node to place childnodes into.

my php is the problem

I am trying to write the formatted xml from flash, to the .xml file between <chat> and </chat> so that the new one is on the top of the older entries

<chat>
<new></new>
<old></old>
</chat>


I am trying to use preg_match to locate "<chat>" so i can place the entry behind it, to no avail,

my php looks like this

$filename = "chat.xml";
$fp = fopen( $filename,"r");
$OldData = fread($fp, filesize('chat.xml'));
preg_match ("/<chat>/s", $OldData, $matches);
fclose( $fp );

not really sure what is needed for preg_match
and then for $new in the next section, which variables to use to write the new xml

$Input = $GLOBALS["HTTP_RAW_POST_DATA"];
$New = " ";
$fp = fopen( $filename,"w+");
fwrite($fp, $New, 80000);
fclose( $fp );


any help would be great, i have seen alot of different posts on this subject, but i am new at php and cant seem to get it to work

THOR
09-06-2002, 06:17 PM
ok instead of trying to write it in a certain spot,
i did the half @$$ed work around, until i can find a better way,

1.flash sends xml formatted text to php,

2. php opens a text file, gets the old data, adds the new data in front and resaves

3 same php opens an xml file, grabs the text file data, and places it between "<?xml version="1.0"><chat>" and "</chat>. rewriting it every time new data is added

now i am gonna bring this xml data back to flash for viewing,

if anyone knows of a better way, and knows of a place to get a good straight jacket after working on this FOREVER. i am all ears

please give opinions, since this is my first php xml flash experience

i know there is an easier way, but this was all i could get to work

JerryScript
09-06-2002, 06:55 PM
Hmmm, doesn't w+ in your fopen statment truncate the file to zero? Perhaps a+ would be better with a rewind().

As for the matching after you've read it in, this should work:

$breakitup$=split("<chat>", $contents);
$newcontents=$breakitup[0]."<chat>".$newstuff.$breakitup[1];

This splits the contents (your xml file) into two chunks at the instance(s) of <chat>. It then creates the file's newcontents by adding the first chunk, the <chat> tag, the newstuff, then the rest.

Jerry

THOR
09-06-2002, 07:35 PM
thanks for the reply jerry, not sure what you mean with the "w+"
and "a+" since i am new to programming in php, i just put what i found in different tuts and posts.

my php code is this :

$filename = "chat.txt";
$fp = fopen( $filename,"r");
$OldData = fread($fp, filesize('chat.txt'));
fclose( $fp );
$Input = $GLOBALS["HTTP_RAW_POST_DATA"];
$New = "$Input$OldData";
echo "$New";
$fp = fopen( $filename,"w+");
fwrite($fp, $New);
fclose( $fp );

$filename = "chat.xml";
$fp = fopen( $filename,"w+");
$XmlHead = "<?xml version=\"1.0\"?><chat>";
$XmlEnd = "</chat>";
$newXml = "$XmlHead$New$XmlEnd";
fwrite($fp, $newXml);
fclose( $fp );

the other problem i have is, if i use your code to search for <chat>, and there is nothing in the node yet, it echos as

<?xml version="1.0"?>
<chat />

which means it wont find <chat> correct?

my current code works, but what would be the best way to do this?

JerryScript
09-07-2002, 10:25 PM
Sorry, was assuming the xml chat tags would be there.

As for the best way, that depends on too many variables for me to say. I say, if the way you are using works, and doesn't seem to strain the system, then it's the best. :)

Jerry