Voting

: max(nine, zero)?
(Example: nine)

The Note You're Voting On

ct
13 years ago
<?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();

?>

<< Back to user notes page

To Top