Node variables - FreeMarker Manual

- Home
- Manual
- Java API

- Contribute
- Report a Bug
- Download

Manual
Previous Next

# Node variables

A node variable embodies a node in a tree structure. Node variables were introduced to help the handling of XML documents in the data-model, but they can be used for the modeling of other tree structures as well. For more information about nodes from the point of view of the template language read this earlier section.

A node variable has the following properties, provided by the methods of `TemplateNodeModel` interface:

- Basic properties:

  - `TemplateSequenceModel getChildNodes()`: A node has sequence of children (except if the node is a leaf node, in which case the method return an empty sequence or null). The child nodes should be node variables as well.

  - `TemplateNodeModel getParentNode()`: A node has exactly 1 parent node, except if the node is root node of the tree, in which case the method returns `null`.

- Optional properties. If a property does not make sense in the concrete use case, the corresponding method should return `null`:

  - `String getNodeName()`: The node name is the name of the macro, that handles the node when you use `recurse` and `visit` directives. Thus, if you want to use these directives with the node, the node name is required.

  - `String getNodeType()`: In the case of XML: `"element"`, `"text"`, `"comment"`, ...etc. This information, if available, is used by the `recurse` and `visit` directives to find the default handler macro for a node. Also it can be useful for other application specific purposes.

  - `String getNamespaceURI()`: The node namespace (has nothing to do with FTL namespaces used for libraries) this node belongs to. For example, in the case of XML, this is the URI of the XML namespace the element or attribute belongs to. This information, if available, is used by the `recurse` and `visit` directives to find the FTL namespaces that store the handler macros.

On the FTL side, the direct utilization of node properties is done with node built-ins, and with the `visit` and `recurse` macros.

In most use cases, variables that implement `TemplateNodeModel`, implement other interfaces as well, since node variable properties just provide the basic infrastructure for navigating between nodes. For a concrete example, see how FreeMarker deals with XML.

Previous Next