Skip to content

Instantly share code, notes, and snippets.

@dahlia
Created March 25, 2010 06:09
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 dahlia/343245 to your computer and use it in GitHub Desktop.
Save dahlia/343245 to your computer and use it in GitHub Desktop.
xmlbuild.php
<?php
class XmlBuilder {
function __call($tag, array $contents) {
$content = '';
foreach ($contents as $c) {
if ($c instanceof XmlBuilder_Element) {
$content .= $c;
} else if (is_array($c)) {
$attrs = array_merge($array, $c);
} else {
$content .= htmlsepcialchars($c);
}
}
foreach ($attrs as $name => $value) {
$attrstr[] = $name . '="' . htmlspecialchars($value) . '"'
}
return "<$tag " . join(' ', $attrstr) . ($content == '' ? " />" : ">$content</$tag>");
}
}
class XmlBuilder_Element {
public $content;
function __construct($content) {
$this->content = $content;
}
function __toString() {
return $this->content;
}
}
#####################################
$x = new XmlBuilder;
$x->element(array('attr' => 'value'),
$x->child('nodes'),
$x->child2());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment