added a toXml() method to Node

This commit is contained in:
Fabien Potencier
2010-06-03 09:27:49 +02:00
parent 8b1105b132
commit e84eaba2b5
+26
View File
@@ -63,6 +63,32 @@ class Twig_Node implements Twig_NodeInterface, ArrayAccess, Countable, Iterator
return implode("\n", $repr);
}
public function toXml($asDom = false)
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$dom->appendChild($xml = $dom->createElement('twig'));
$xml->appendChild($node = $dom->createElement('node'));
$node->setAttribute('class', get_class($this));
foreach ($this->attributes as $name => $value) {
$node->appendChild($attribute = $dom->createElement('attribute'));
$attribute->setAttribute('name', $name);
$attribute->appendChild($dom->createTextNode($value));
}
foreach ($this->nodes as $name => $n) {
$child = $n->toXml(true)->getElementsByTagName('node')->item(0);
$child = $dom->importNode($child, true);
$child->setAttribute('name', $name);
$node->appendChild($child);
}
return $asDom ? $dom : $dom->saveXml();
}
public function compile($compiler)
{
foreach ($this->nodes as $node) {