Skip to content

Instantly share code, notes, and snippets.

@ericmulder
Created February 26, 2016 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericmulder/1e3bf4592d7c0463127d to your computer and use it in GitHub Desktop.
Save ericmulder/1e3bf4592d7c0463127d to your computer and use it in GitHub Desktop.
<?php
class BetterXMLElement extends SimpleXMLElement {
//appends this xml to a parent
public function appendToSimpleXML(SimpleXMLElement $parent) {
$toDom = dom_import_simplexml($parent);
$fromDom = dom_import_simplexml($this);
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}
//appends $child SimpleXMLElement to this object
public function appendChild(SimpleXMLElement $child) {
$toDom = dom_import_simplexml($this);
$fromDom = dom_import_simplexml($child);
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}
}
//use like
$xml = new \BetterXMLElement('<financials/>');
$xml->addChild('matchtype', 'customersupplier');
$xml->addChild('duedays', 14);
$xml->addChild('payavailable', 'false');
$xml->addChild('meansofpayment', 'none');
$xml->addChild('collectionschema', 'core');
$customer = new \BetterXMLElement('<customer/>');
$customer->appendChild($xml);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment