开发者

FreeMarker Template JSON - Give it a node?nextSibling to find out, if the current node have a sibling?

I want to convert a XML File to JSON. The problem is, I have the structure

<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>

I want it to the JSON Format like:

{
  "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
              } ** , ** ]

But there is always a comma "," after the laste children. With freemarker there is a way to find out if the node have a parent, children or whatever, like node?parent, node?children. But no chance to find out, if there it has a sibling.

How does freemarker know, if the current node has a sibling?


There are two special loop variables available inside the list loop:

item_index: This is a numerical value that contains the index of the current item being stepped over in the loop.

item_has_next: Boolean value that tells if the current item the last in the sequence or not.

Example:

<#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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜