I am traversing an XML tree visiting each node using DFS. The output that I get is not what I expected.
object Main extends App {
lazy val testXml =
<vehicles>
<vehicle>
gg
</vehicle>
<variable>
</variable>
</vehicles>
traverse.dfs(testXml.head)
}
object traverse {
def dfs(node: Node): Unit = {
println("==============")
println(node.label + ">>>" + node.child + ">>>" + node.child.size)
node.child.map(child => {
dfs(child)
})
}
}
Output:
==============
vehicles>>>ArrayBuffer(
, <vehicle>
gg
</vehicle>,
, <variable>
</variable>,
)>>>5
==============
#PCDATA>>>List()>>>0
==============
vehicle>>>ArrayBuffer(
gg
)>>>1
==============
#PCDATA>>>List()>>>0
==============
#PCDATA>>>List()>>>0
==============
variable>>>ArrayBuffer(
)>>>1
==============
#PCDATA>>>List()>>>0
==============
#PCDATA>>>List()>>>0
Process finished with exit code 0
If you take a look at the output, for the first element (vehicles
) it says it has 5 children. If you print the children, two children (the first and the last) are empty.
I want the traversal to visit vehicles
then vehicle
then gg
and finally variable
.
Any advice with this is appreciated. Thanks.