I am currently implementing DFS traversal of an xml such that it goes to each leaf node and generates the path to the leaf node.
Given XML:
<vehicles>
<vehicle>
gg
</vehicle>
<variable>
</variable>
</vehicles>
Output (Somthing like):
Map("gg" -> "vehicles/vehicle", "" -> "vehicles/variable")
It would be great if there is a library available that does this so I dont have to maintain the code.
Thanks. Any help is appreciated.
Here is a solution using standard scala xml library, prints out a map of paths -> "node text"
For those that just want a function to use or a more XPath friendly solution
I have created a repository that extends the below code and should generate correct XPaths, but am leaving the below code as is since it is relatively simple and is a good place to start for understanding the code. The repo is on github.
Answer
Here's an implementation inspired by @Samar's answer that generates XPaths (so far without proper attribute notation), is tail-recursive, handles attributes, and doesn't use a mutable collection:
Run like this:
Suggestions welcome. I plan to fix attribute handling to generate proper XPaths that depend on attribute selection, and may also try to handle recursive XPaths in some sensible way, but I suspect this will greatly increase the code size so I wanted to go ahead and paste this for now.