Other node tree representations =============================== It is possible to convert the AST in several textual representations, which serve different uses. Simple serialization -------------------- It is possible to serialize the node tree using `serialize()` and also unserialize it using `unserialize()`. The output is not human readable and not easily processable from anything but PHP, but it is compact and generates fast. The main application thus is in caching. Human readable dumping ---------------------- Furthermore it is possible to dump nodes into a human readable form using the `dump` method of `PHPParser_NodeDumper`. This can be used for debugging. ```php parse(new PHPParser_Lexer($code)); echo '
' . htmlspecialchars($nodeDumper->dump($stmts)) . '
'; } catch (PHPParser_Error $e) { echo 'Parse Error: ', $e->getMessage(); } ``` The above output will have an output looking roughly like this: ``` array( 0: Stmt_Function( byRef: false params: array( 0: Param( name: msg default: null type: null byRef: false ) ) stmts: array( 0: Stmt_Echo( exprs: array( 0: Expr_Variable( name: msg ) 1: Scalar_String( value: ) ) ) ) name: printLine ) 1: Expr_FuncCall( name: Name( parts: array( 0: printLine ) ) args: array( 0: Arg( value: Scalar_String( value: Hallo World!!! ) byRef: false ) ) ) ) ``` Serialization to XML -------------------- It is also possible to serialize the node tree to XML using `PHPParser_Serializer_XML->serialize()` and to unserialize it using `PHPParser_Unserializer_XML->unserialize()`. This is useful for interfacing with other languages and applications or for doing transformation using XSLT. ```php parse(new PHPParser_Lexer($code)); echo '
' . htmlspecialchars($serializer->serialize($stmts)) . '
'; } catch (PHPParser_Error $e) { echo 'Parse Error: ', $e->getMessage(); } ``` Produces: ``` msg msg printLine printLine Hallo World!!! ```