From: Fabien Potencier Date: Thu, 3 Jun 2010 07:27:49 +0000 (+0200) Subject: added a toXml() method to Node X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=e84eaba2b58200987c6c96a1bbe8208b9b0bfbb8;p=web%2Fkonrad%2Ftwig.git added a toXml() method to Node --- diff --git a/lib/Twig/Node.php b/lib/Twig/Node.php index f769077..02efca3 100644 --- a/lib/Twig/Node.php +++ b/lib/Twig/Node.php @@ -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) {