Skip to content

Instantly share code, notes, and snippets.

@ThomasWeinert
Created June 10, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ThomasWeinert/c071e26a4af351bd143c to your computer and use it in GitHub Desktop.
Save ThomasWeinert/c071e26a4af351bd143c to your computer and use it in GitHub Desktop.
<?php
class MySimpleXMLElement extends SimpleXMLElement {
public function addChild($name, $value = null, $namespace = null) {
$result = parent::addChild($name, null, $namespace);
if (isset($value)) {
$node = dom_import_simplexml($result);
$node->appendChild($node->ownerDocument->createTextNode($value));
}
return $result;
}
}
$element = new MySimpleXMLElement('<foo/>');
$element->addChild('bar', 'A & B');
$element->addChild('bar', 'C & D');
echo $element->asXml();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment