Skip to content

Instantly share code, notes, and snippets.

@adamcoulombe
Created April 21, 2012 01:44
Show Gist options
  • Save adamcoulombe/2433202 to your computer and use it in GitHub Desktop.
Save adamcoulombe/2433202 to your computer and use it in GitHub Desktop.
Reading and Saving XML files with PHP DOMDocument
<?xml version="1.0" encoding="utf-8"?>
<root>
<color type="white"/>
</root>
<?php
$doc = new DOMDocument();
$doc->load("myxml.xml");
$root = $doc->firstChild;
$colors= $root->getElementsByTagName("color");
foreach($colors as $color){
print_r($color->getAttribute("type"));
}
$new_color = $doc->createElement('color');
$new_color->setAttribute("type","blue");
$root->insertBefore($new_color,$root->firstChild);
$doc->save("myxml.xml");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment