【问题标题】:XML tree traversal using tail recursion in Scala在 Scala 中使用尾递归遍历 XML 树
【发布时间】:2014-07-01 23:58:51
【问题描述】:

假设我们有一个类似的 XML

<a id="one">
 <aa id="one -depth one"> 
    <aaa id="one -depth two">
    </aaa>
 </aa>
 <bb id="two -depth one"> 
    <bbb id="two -depth two">
       <bbbb id="bbbb depth three">
       </bbbb>
    </bbb>
 </bb>

</a>
<b id="two"></b>
<c id="three"></c>
<d id="four"></d>
<e id="five"></e>'

然后,我希望通过使用 Scala 的尾递归或任何其他方式获得类似于以下的输出,同时考虑性能和代码质量。节点的深度可以是动态的(不固定,可以到任意深度)

List(
Object(A, List(
       Object(AA, List(
                     Object(AAA, List())
                       )
              ),
       Object(BB, List(
                     Object(BBB, List(
                                    Object(BBBB, List())
                            )        )
                       )
              )
           )
   ),
Object(B, List()),
Object(C, List()),
Object(D, List()),
Object(E, List())
)

任何建议甚至某种伪代码都将不胜感激。

【问题讨论】:

  • 您的数据来自哪里?您只是想从代码中构造一些任意结构吗?你看scala.xml了吗?
  • 也在使用这些库。但是在处理/处理 XML 的动态深度位时遇到了困难。嗯...
  • 嗨 nietaki,有没有办法为某些 URL 的 XML 加载设置标头?

标签: xml scala recursion functional-programming tail


【解决方案1】:

我将在这里用 Scala 的 PSEUDO 代码回答我自己的问题。如果能提供更好的替代方案就好了。

def walkXmlPath(someXMLNodes: Node) = {
   // Check if a node has a child/children nodes

   // If there are children nodes, then iterate over them

   // Inside the iteration, do some necessary operations and call 'def walkXmlPath(...)' until the complete tree walk 

   // Return some processed nodes' extracts
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-12
    • 2014-03-11
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    相关资源
    最近更新 更多