Jump to content


Photo

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


  • Please log in to reply
9 replies to this topic

#1 maexus

maexus
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts

Posted 31 July 2006 - 03:28 PM

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

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 July 2006 - 03:48 PM

You might want to add FILE_APPEND as the third paramter for file_put_contents. So it should be like this:
file_put_contents($file, $log->asXML(), FILE_APPEND);


#3 maexus

maexus
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts

Posted 31 July 2006 - 03:56 PM

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:

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

Which of course is not valid XML. I want to have this as the base XML:

<?xml version="1.0"?>
<log>
<messagecounter value="0">
</log>

and output this:

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

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.

#4 maexus

maexus
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts

Posted 31 July 2006 - 03:57 PM

BTW, I do not want to use a database. XML only.

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 July 2006 - 04:03 PM

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:
$xmld = file_get_contents($file);
$xmld = str_replace('</log>', '', $xmld);
file_put_contents($xmld . $file . '</log>', $log->asXML())



#6 maexus

maexus
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts

Posted 31 July 2006 - 04:05 PM

That would work.. There should be an easier way to work with XML in PHP, esp if you think about what XML.

#7 maexus

maexus
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts

Posted 31 July 2006 - 04:19 PM

Ok, here is what I have:

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

and I get this error:


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

Line 19 is:

$log = new SimpleXMLElement($xml);

Here is my XML file

<?xml version="1.0"?>
<log>
<messagecounter value="0"/>
</log>

Help?

#8 maexus

maexus
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts

Posted 31 July 2006 - 04:23 PM

Changed it up a bit:

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

Now getting this error:

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


#9 maexus

maexus
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts

Posted 31 July 2006 - 05:29 PM

Ok, forget all that above. Here is what I have it down too but it still doesn't work:

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

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

#10 maexus

maexus
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts

Posted 31 July 2006 - 09:31 PM

bump




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users