Skip to content

Instantly share code, notes, and snippets.

@d1i1m1o1n
Last active August 29, 2015 14:23
Show Gist options
  • Save d1i1m1o1n/913b58d504cda9f27782 to your computer and use it in GitHub Desktop.
Save d1i1m1o1n/913b58d504cda9f27782 to your computer and use it in GitHub Desktop.
Work with xml using simpleXML
$xml = simplexml_load_file("news.xml") or die("Error: Cannot create object");
$id = count($xml->channel->item);
$item = $xml->channel->addChild('item', '');
$item->addAttribute('id', $id);
$item->addChild('date', $_POST['date']);
$item->addChild('title', $_POST['title']);
$item->addChild('description', $_POST['description']);
$xml->asXml('news.xml');
$xml = simplexml_load_file("news.xml") or die("Error: Cannot create object");
$id = $_POST['id'];
unset($xml->xpath("//item[@id='$id']")[0]->{0});
$xml->asXml('news.xml');
$xml = simplexml_load_file("news.xml") or die("Error: Cannot create object");
$id = $_POST['id'];
$item = $xml->xpath("//item[@id='$id']");
$item[0]->date = $_POST['date'];
$item[0]->title = $_POST['title'];
$item[0]->description = $_POST['description'];
$xml->asXml('news.xml');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment