maexus Posted May 19, 2006 Share Posted May 19, 2006 I am writing a databaseless shoutbox, which the txt version is done. I'm converting it to XML so I can add features such as search, delete, edit, ect... I can read XML using SimpleXML just fine, no problems. The problem I run into is writing xml. I want to be able to write to the XML in very much the same way I would using INSERT in SQL. Append the file using the new data but add it on at the end of the file but before the end root element. Any help would be great. :-D Quote Link to comment https://forums.phpfreaks.com/topic/10035-simplexml-using-xml-like-sql-for-insert/ Share on other sites More sharing options...
maexus Posted May 20, 2006 Author Share Posted May 20, 2006 bump Quote Link to comment https://forums.phpfreaks.com/topic/10035-simplexml-using-xml-like-sql-for-insert/#findComment-37426 Share on other sites More sharing options...
yonta Posted May 20, 2006 Share Posted May 20, 2006 I also have the same question. Is it possible to write in a xml file at the given element i want like mysql, changing only that value or do i have to load the whole xml into a variable and rewrite it whole again with the changes i want?Thanx Quote Link to comment https://forums.phpfreaks.com/topic/10035-simplexml-using-xml-like-sql-for-insert/#findComment-37450 Share on other sites More sharing options...
448191 Posted May 20, 2006 Share Posted May 20, 2006 I'm currently working on accomplishing similar tasks using the built-in DOM class set.If you like I can help you out using that method. I'm not really familiar with simpleXML's use. Quote Link to comment https://forums.phpfreaks.com/topic/10035-simplexml-using-xml-like-sql-for-insert/#findComment-37451 Share on other sites More sharing options...
maexus Posted May 20, 2006 Author Share Posted May 20, 2006 I don't have my mind set on SimpleXML so if you have a method to do what I need to do. [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /] Please share. It would be very much appreciated.[!--quoteo(post=375525:date=May 20 2006, 11:21 AM:name=yonta)--][div class=\'quotetop\']QUOTE(yonta @ May 20 2006, 11:21 AM) [snapback]375525[/snapback][/div][div class=\'quotemain\'][!--quotec--]I also have the same question. Is it possible to write in a xml file at the given element i want like mysql, changing only that value or do i have to load the whole xml into a variable and rewrite it whole again with the changes i want?Thanx[/quote]I believe you can change an XML entry easily enough. All you have to do, if you use SimpleXML is to specify which entry you want to change by either using [#] or using XQuery. I believe you could use XQuery for that, which is very similar to SQL queries in syntax. Quote Link to comment https://forums.phpfreaks.com/topic/10035-simplexml-using-xml-like-sql-for-insert/#findComment-37459 Share on other sites More sharing options...
448191 Posted May 21, 2006 Share Posted May 21, 2006 Normally I don't do this, but since I could use the practice with DOM, I'm going to provide you with a working class to read and write XML for the purpose of a shoutbox. So you're in luck. Oh, I almost forgot: you have to have php5, or this class won't work![div align=\"center\"][b][!--sizeo:4--][span style=\"font-size:14pt;line-height:100%\"][!--/sizeo--]Class shoutXML :[!--sizec--][/span][!--/sizec--][/b][/div][code]class shoutXML extends DOMDocument{ private $filePath; private $rootNode; public $shoutCount; public $limitStorage; public $limitIds; function __construct($file,$limitStorage) { $this->filePath = $file; //$this->validateOnParse = true; $this->preserveWhitespace = false; $this->formatOutput = true; $this->limitStorage = $limitStorage; $this->limitIds = 100; parent::__construct(); if(file_exists($file)){ parent::load($file); $this->rootNode = parent::getElementById('root'); } else { //Create root element: $this->rootNode = parent::createElement(substr($file,strrpos($file,'/' )+1,strlen($file))); $this->rootNode->setAttribute('xml:id', 'root'); parent::appendChild($this->rootNode); } } function __destruct() { parent::save($this->filePath); } public function removeShout($id) { return $this->rootNode->removeChild(parent::getElementById($id)); } public function editShoutText($id,$newText) { if($element = parent::getElementById($id)) { $element->nodeValue = $newText; return true; } else { return false; } } public function addShout($user,$text) { $newnode = parent::createElement('shout'); $newnode->setAttribute('xml:id', 'id'.(self::getLastNr()+1)); $newnode->setAttribute('nr', self::getLastNr()+1); $newnode->setAttribute('time', time()); $newnode->setAttribute('user', $user); $newnode->nodeValue = $text; //Append child element: $this->rootNode->appendChild($newnode); //Pop shouts: for($count = self::calculateCount(); $count >= ($this->limitStorage); $count--) { $this->rootNode->removeChild($this->rootNode->firstChild); } } private function getLastNr(){ if($child = $this->rootNode->lastChild) { if($child->getAttribute('nr')>= $this->limitIds) { return false; //Start over. } else { return $child->getAttribute('nr'); } } else { return false; } } private function calculateCount() { if($child = $this->rootNode->firstChild) { $firstRowNr = $child->getAttribute('nr'); $lastRowNr = $this->rootNode->lastChild->getAttribute('nr'); //If the first nr is bigger than the last, count has been reset. if($firstRowNr>$lastRowNr) { //Calculate: limit - first_row_nr + last_row_nr +1. return $this->limitIds-$firstRowNr+$lastRowNr+1; } else { //Calculate: last_row_nr - first_row_nr. return $lastRowNr-$firstRowNr; } } else { return false; } } public function fetchShouts($limit) { $arr = array(); //Fetch in reversed order: for($child = $this->rootNode->lastChild; count($arr)<$limit; $child = $child->previousSibling) { $id = $child->getAttribute('id'); $arr[$id]['nr'] = $child->getAttribute('nr'); $arr[$id]['user'] = $child->getAttribute('user'); $arr[$id]['time'] = $child->getAttribute('time'); $arr[$id]['text'] = $child->nodeValue; } //return the result; return $arr; }}[/code][div align=\"center\"][b][!--sizeo:4--][span style=\"font-size:14pt;line-height:100%\"][!--/sizeo--]Usage:[!--sizec--][/span][!--/sizec--][/b][/div][b]shoutXML::__construct(file_to_store_shouts, limit_number_of_shouts_to_store)[/b][code]$shouts = new shoutXML($_SERVER['DOCUMENT_ROOT'].'/shout.xml',4);[/code][b]shoutXML::addShout(username, shout_text)[/b][code]$shouts->addShout('448191','What\'s up?');[/code][b]shoutXML::fetchShouts(limit_number_shouts_to_fetch)[/b][code]$arr = $shouts->fetchShouts(3);[/code][b]shoutXML::removeShout(shout_id)[/b][code]$shout->removeShout($_GET['shout_id']);[/code][b]shoutXML::editShoutText(shout_id,new_shout_text)[/b][code]$shout->editShoutText($_GET['shout_id'],'Censored!');[/code][div align=\"center\"][b][!--sizeo:4--][span style=\"font-size:14pt;line-height:100%\"][!--/sizeo--]Example:[!--sizec--][/span][!--/sizec--][/b][/div][code]$shouts = new shoutXML($_SERVER['DOCUMENT_ROOT'].'/shout.xml',4);$shouts->addShout('448191','What\'s up?');$shouts->addShout('maexus','Nothing much.');$shouts->addShout('448191','OK, whatever then.');$shouts->addShout('maexus','Chill out!');$shouts->addShout('448191','I am chilled out;)');$arr = $shouts->fetchShouts(3);echo '<pre>';print_r($arr);echo '</pre>';[/code]This prints:[code]Array( [0] => Array ( [time] => 1148198445 [user] => 448191 [text] => What's up? ) [1] => Array ( [time] => 1148198445 [user] => maexus [text] => Nothing much. ) [2] => Array ( [time] => 1148198445 [user] => 448191 [text] => OK, whatever then. ))[/code]This writes:[code]<?xml version="1.0"?><shout.xml> <shout id="0" time="1148198445" user="448191">What's up?</shout> <shout id="1" time="1148198445" user="maexus">Nothing much.</shout> <shout id="2" time="1148198445" user="448191">OK, whatever then.</shout> <shout id="3" time="1148198445" user="maexus">Chill out!</shout></shout.xml>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10035-simplexml-using-xml-like-sql-for-insert/#findComment-37616 Share on other sites More sharing options...
448191 Posted May 21, 2006 Share Posted May 21, 2006 Did an almost complete rewrite. It should work proper now.Have fun. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/10035-simplexml-using-xml-like-sql-for-insert/#findComment-37622 Share on other sites More sharing options...
maexus Posted May 21, 2006 Author Share Posted May 21, 2006 I have to goto work today so no time for coding but I really appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/10035-simplexml-using-xml-like-sql-for-insert/#findComment-37693 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.