Jump to content

Archived

This topic is now archived and is closed to further replies.

maexus

Using SimpleXML to write to a file, not overwrite whats there, just append.

Recommended Posts

[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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
That would work.. There should be an easier way to work with XML in PHP, esp if you think about what XML.

Share this post


Link to post
Share on other sites
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 19

Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: in C:\Program Files\xampp\htdocs\xmlio\index.php on line 19

Warning: SimpleXMLElement::__construct() [function.SimpleXMLElement---construct]: ^ in C:\Program Files\xampp\htdocs\xmlio\index.php on line 19

Fatal 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?

Share this post


Link to post
Share on other sites
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 20

Warning: Attempt to assign property of non-object in C:\Program Files\xampp\htdocs\xmlio\index.php on line 21

Warning: Attempt to assign property of non-object in C:\Program Files\xampp\htdocs\xmlio\index.php on line 22

Warning: Attempt to assign property of non-object in C:\Program Files\xampp\htdocs\xmlio\index.php on line 23

Fatal error: Call to undefined method stdClass::asXML() in C:\Program Files\xampp\htdocs\xmlio\index.php on line 24
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.