Re: Using XML as a data-model

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Friday, February 20, 2009, 11:50:42 PM, Kirill wrote:
> Hello,
> I am using an XML as a data-model, similar to this:
>
> Map root = new HashMap();
> root.put("doc", freemarker.ext.dom.NodeModel.parse(new
> File("the/path/of/the.xml")));
>
> Then, I can recurse through the document with <#recurse doc>, where
> 'doc' is a key name from the Map.
>
> The question is - how can I refer to that 'doc' variable? For example,
> if I don't know its' value and want to recurse through the XML
> data-model.
>
> So, if I do something like:
>
> root.put(stringKeyName, freemarker.ext.dom.NodeModel.parse(new
> File("the/path/of/the.xml")));
>
> I need to be able to recurse in this manner:
>
> <#recurse stringKeyName>

I'm not sure I understand it... You put something into the data-mode
with a name that you don't know at the time of template authoring, and
then still want to access that variable? Of course that's only
possible if you add the *name* of the variable to data-model as well:

  root.put("keyName", stringKeyName);

and the in the template:

  <#recurse .vars[keyName] />

but then it's simpler if you just chose a fixed alias name:

  TemplateModel val = freemarker.ext.dom.NodeModel.parse(new
      File("the/path/of/the.xml"))
  root.put(stringKeyName, val);
  root.put("theDoc", val);

and

  <#recurse theDoc />

Last not least, the template can discover the data-model content
itself, for example:

  <#list .data_model?keys as k>
    <#if .data_model[k]?is_node>
      <#recurse .data_model[k]>
    </#if>
  </#list>

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.