note 105121 added to class.iterator
| From: | ct at osu1 dot php dot net | Date: | Thu, 28 Jul 2011 19:00:28 +0000 |
| Subject: | note 105121 added to class.iterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181964@lists.php.net to get a copy of this message | ||
<?php
/*
* An implementation of the Iterator
* with simpleXML to remove a node and generate a new XML file.
*
* project.xml file:
* <?xml version="1.0" encoding="UTF-8"?>
* ...
* <data>
* <item>
* <value>one</value>
* </item>
* <item>
* <value>two</value>
* </item>
* ...
* </data>
*
*/
class parseXML implements Iterator {
private $position;
private $xml;
public $item;
public function __construct() {
$this->position = 0;
$this->xml = simplexml_load_file('project.xml');
}
public function unsetItem() {
foreach ($this as $key => $value) {
if ($value->value == $this->item ) {
unset($this->xml->data->item[$key]);
}
}
$this->mkXML();
}
public function mkXML() {
file_put_contents('project.xml', $this->xml->asXML() );
}
function rewind() {
$this->position = 0;
}
function current() {
return $this->xml->data->item[$this->position];
}
function key() {
return $this->position;
}
function next() {
++$this->position;
}
function valid() {
return isset($this->xml->data->item[$this->position]);
}
}
$itemRemove = new parseXML();
$itemRemove->item = "one";
$itemRemove->unsetItem();
?>
----
Server IP: 69.147.83.197
Probable Submitter: 190.244.141.26
----
Manual Page -- http://www.php.net/manual/en/class.iterator.php
Edit -- https://master.php.net/note/edit/105121
Del: integrated -- https://master.php.net/note/delete/105121/integrated
Del: useless -- https://master.php.net/note/delete/105121/useless
Del: bad code -- https://master.php.net/note/delete/105121/bad+code
Del: spam -- https://master.php.net/note/delete/105121/spam
Del: non-english -- https://master.php.net/note/delete/105121/non-english
Del: in docs -- https://master.php.net/note/delete/105121/in+docs
Del: other reasons-- https://master.php.net/note/delete/105121
Reject -- https://master.php.net/note/reject/105121
Search -- https://master.php.net/manage/user-notes.php