maexus Posted July 31, 2006 Share Posted July 31, 2006 [code]<?php$file = "log.xml";$ip = $_SERVER['REMOTE_ADDR'];/* What the xml file will look like, except more messages<log><messagecount="#"/><message id="#"><username></username><password></password><ip></ip><text></text></message></log>*/if(isset($_POST['submit'])){ $log = simplexml_load_file($file); $log->message['id'] = $log->messagecount + 1; $log->message->username = $_POST['username']; $log->message->password = $_POST['password']; $log->message->ip = $_SERVER['REMOTE_ADDR']; $log->message->text = $_POST['text']; file_put_contents($file, $log->asXML()); // <---- This is what I'm having trouble with. }?><form action="index.php" method="post"><input type="text" name="username"><input type="password" name="password"><textarea name="text"></textarea><input type="submit" name="submit"><form>[/code]Code so far. It works at writing xml, except it erases the current message and overwrites with the new one that's submitted. I have a feeling this has something to do with file_put_contents($file, $log->asXML());I don't know what else to do to make this work. Quote Link to comment https://forums.phpfreaks.com/topic/16111-using-simplexml-to-write-to-a-file-not-overwrite-whats-there-just-append/ Share on other sites More sharing options...
wildteen88 Posted July 31, 2006 Share Posted July 31, 2006 You might want to add FILE_APPEND as the third paramter for file_put_contents. So it should be like this:[code=php:0]file_put_contents($file, $log->asXML(), FILE_APPEND);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16111-using-simplexml-to-write-to-a-file-not-overwrite-whats-there-just-append/#findComment-66414 Share on other sites More sharing options...
maexus Posted July 31, 2006 Author Share Posted July 31, 2006 It's not that simple, I don't want to append the file, I want to append the xml data. XML has to have the parent tag closed around the data. Doing it your way produces:[code]<?xml version="1.0"?><log></log><?xml version="1.0"?><log><message id="1"><username>dfdf</username><password>dfd</password><ip>127.0.0.1</ip><text>dfsdf</text></message></log>[/code]Which of course is not valid XML. I want to have this as the base XML:[code]<?xml version="1.0"?><log><messagecounter value="0"></log>[/code]and output this:[code]<?xml version="1.0"?><log><messagecounter value="1"><message id="1"><username>Kyle</username><password>password</password><ip>127.0.0.1</ip><text>Hello, World!</text></message></log>[/code]Each time the form is submitted, it adds a new message, adding +1 to the message counter and leaving the base tag and existing data intact. None of the guides I have read go into detail like that. At all. I don't care about writing a simple xml line or reading an xml file, I need something more complex. Quote Link to comment https://forums.phpfreaks.com/topic/16111-using-simplexml-to-write-to-a-file-not-overwrite-whats-there-just-append/#findComment-66422 Share on other sites More sharing options...
maexus Posted July 31, 2006 Author Share Posted July 31, 2006 BTW, I do not want to use a database. XML only. Quote Link to comment https://forums.phpfreaks.com/topic/16111-using-simplexml-to-write-to-a-file-not-overwrite-whats-there-just-append/#findComment-66423 Share on other sites More sharing options...
wildteen88 Posted July 31, 2006 Share Posted July 31, 2006 Prehaps you'll want to fetch the contents of the file first. Then use str_replace to remove </log> then you add it back in with file_put_contents like so:[code=php:0]$xmld = file_get_contents($file);$xmld = str_replace('</log>', '', $xmld);file_put_contents($xmld . $file . '</log>', $log->asXML())[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16111-using-simplexml-to-write-to-a-file-not-overwrite-whats-there-just-append/#findComment-66428 Share on other sites More sharing options...
maexus Posted July 31, 2006 Author Share Posted July 31, 2006 That would work.. There should be an easier way to work with XML in PHP, esp if you think about what XML. Quote Link to comment https://forums.phpfreaks.com/topic/16111-using-simplexml-to-write-to-a-file-not-overwrite-whats-there-just-append/#findComment-66429 Share on other sites More sharing options...
maexus Posted July 31, 2006 Author Share Posted July 31, 2006 Ok, here is what I have:[code]<?php$file = "log.xml";$ip = $_SERVER['REMOTE_ADDR'];/*<log><messagecount="#"/><message id="#"><username></username><password></password><ip></ip><text></text></message></log>*/if(isset($_POST['submit'])){ $xml = file_get_contents($file); $xml = str_replace('</log>', '', $xml); $log = new SimpleXMLElement($xml); $log->message['id'] = $log->messagecounter['value'] + 1; $log->message->username = $_POST['username']; $log->message->password = $_POST['password']; $log->message->ip = $_SERVER['REMOTE_ADDR']; $log->message->text = $_POST['text']; file_put_contents($file . $xml . '</log>', $log->asXML());}?><form action="index.php" method="post"><input type="text" name="username"><input type="password" name="password"><textarea name="text"></textarea><input type="submit" name="submit"><form>[/code]and I get this error:[code]Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: Entity: line 5: parser error : Premature end of data in tag log line 2 in C:\Program Files\xampp\htdocs\xmlio\index.php on line 19Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: in C:\Program Files\xampp\htdocs\xmlio\index.php on line 19Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: ^ in C:\Program Files\xampp\htdocs\xmlio\index.php on line 19Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\Program Files\xampp\htdocs\xmlio\index.php:19 Stack trace: #0 C:\Program Files\xampp\htdocs\xmlio\index.php(19): SimpleXMLElement->__construct('<?xml version="...') #1 {main} thrown in C:\Program Files\xampp\htdocs\xmlio\index.php on line 19[/code]Line 19 is:[code]$log = new SimpleXMLElement($xml);[/code]Here is my XML file[code]<?xml version="1.0"?><log><messagecounter value="0"/></log>[/code]Help? Quote Link to comment https://forums.phpfreaks.com/topic/16111-using-simplexml-to-write-to-a-file-not-overwrite-whats-there-just-append/#findComment-66434 Share on other sites More sharing options...
maexus Posted July 31, 2006 Author Share Posted July 31, 2006 Changed it up a bit:[code]<?php$file = "log.xml";$ip = $_SERVER['REMOTE_ADDR'];/*<log><messagecount="#"/><message id="#"><username></username><password></password><ip></ip><text></text></message></log>*/if(isset($_POST['submit'])){ $log = simplexml_load_string($xml); $log = str_replace('</log>', '', $log); $log->message['id'] = $log->messagecounter['value'] + 1; $log->message->username = $_POST['username']; $log->message->password = $_POST['password']; $log->message->ip = $_SERVER['REMOTE_ADDR']; $log->message->text = $_POST['text']; file_put_contents($file . $xml . '</log>', $log->asXML());}?><form action="index.php" method="post"><input type="text" name="username"><input type="password" name="password"><textarea name="text"></textarea><input type="submit" name="submit"><form>[/code]Now getting this error: [code]Warning: Attempt to assign property of non-object in C:\Program Files\xampp\htdocs\xmlio\index.php on line 20Warning: Attempt to assign property of non-object in C:\Program Files\xampp\htdocs\xmlio\index.php on line 21Warning: Attempt to assign property of non-object in C:\Program Files\xampp\htdocs\xmlio\index.php on line 22Warning: Attempt to assign property of non-object in C:\Program Files\xampp\htdocs\xmlio\index.php on line 23Fatal error: Call to undefined method stdClass::asXML() in C:\Program Files\xampp\htdocs\xmlio\index.php on line 24[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16111-using-simplexml-to-write-to-a-file-not-overwrite-whats-there-just-append/#findComment-66436 Share on other sites More sharing options...
maexus Posted July 31, 2006 Author Share Posted July 31, 2006 Ok, forget all that above. Here is what I have it down too but it still doesn't work:[code]if(isset($_POST['submit'])){ $import = file_get_contents($file); $import = str_replace('</log>', '', $import); $log = simplexml_load_file($file); $log->message['id'] = $log->messagecounter['value'] + 1; $log->message->username = $_POST['username']; $log->message->password = $_POST['password']; $log->message->ip = $_SERVER['REMOTE_ADDR']; $log->message->text = $_POST['text']; $log->messagecounter['value'] = $log->messagecounter['value'] + 1; file_put_contents($file, $import.$log->asXML()."</log>"); // <---- This is what I'm having trouble with. }[/code]$log->asXML() writes a new XML declaration so for each one added, a new declaration is added. This obviously won't work. Why is this so damn hard. Surely someone has to be working with XML like this in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/16111-using-simplexml-to-write-to-a-file-not-overwrite-whats-there-just-append/#findComment-66479 Share on other sites More sharing options...
maexus Posted July 31, 2006 Author Share Posted July 31, 2006 bump Quote Link to comment https://forums.phpfreaks.com/topic/16111-using-simplexml-to-write-to-a-file-not-overwrite-whats-there-just-append/#findComment-66652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.