【问题标题】:FreeMarker Template JSON - Give it a node?nextSibling to find out, if the current node have a sibling?FreeMarker 模板 JSON - 给它一个节点?nextSibling 找出当前节点是否有兄弟节点?
【发布时间】:2010-06-25 07:35:16
【问题描述】:

我想将 XML 文件转换为 JSON。问题是,我有结构

<node id="1">
    <title>Content ...</title>
</node>

<node id="2">
    <title>Secon ...</title>
    <subnodes>
        <node id="3">
            <title>Secon ...</title>
            <subnodes>
                <node id="4">
                    <title>Secon ...</title>
                </node>
            </subnodes>
        </node>
    </subnodes>
</node>

我希望它为 JSON 格式,例如:

{
  "nodeid": "34",
  "text": "First level",
  "children": [{
        "nodeid": "1",
        "text": "Content ...",
        "leaf": true,
        "children": [{
              "nodeid": "2",
              "text": "Second",
              "leaf": true,
              "children": [{
                "nodeid": "3",
                "text": "Third",
                "leaf": true
              } ** , ** ]

但是在laste children 之后总是有一个逗号“,”。使用 freemarker 可以确定节点是否有父节点、子节点或其他任何节点,例如节点?父节点、节点?子节点。但是没有机会找出它是否有兄弟姐妹。

freemarker 如何知道当前节点是否有兄弟节点?

【问题讨论】:

    标签: xml json freemarker


    【解决方案1】:

    列表循环中有两个特殊的循环变量可用:

    item_index:这是一个数值,包含在循环中被跳过的当前项目的索引。

    item_has_next:布尔值,指示当前项目是否在序列中的最后一个。

    例子:

    <#assign seq = ["winter", "spring", "summer", "autumn"]>
    <#list seq as x>
      ${x_index + 1}. ${x}<#if x_has_next>,</#if>
    </#list>
    

    http://freemarker.sourceforge.net/docs/ref_directive_list.html

    【讨论】:

      猜你喜欢
      • 2014-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多