title: Scala convert XML to key value map
body: <p>Related to <a href="https://stackoverflow.com/questions/39001421/scala-what-is-the-easiest-way-to-get-all-leaf-nodes-and-their-paths-in-an-xml">This topic</a></p>
  
  <p>Problem is as follows, imagine an XML without any particular schema</p>
  
  <pre><code>&lt;persons&gt;
    &lt;total&gt;2&lt;/total&gt;
    &lt;someguy&gt;
       &lt;firstname&gt;john&lt;/firstname&gt;
       &lt;name&gt;Snow&lt;/name&gt;
    &lt;/someguy&gt;
    &lt;otherperson&gt;
       &lt;sex&gt;female&lt;/sex&gt;
    &lt;/otherperson&gt;
  &lt;/persons&gt;
  </code></pre>
  
  <p>For processing I want to have this in a Key Value Map:</p>
  
  <pre><code>"Persons/total" -&gt; 2
  "Persons/someguy/firstname" -&gt; john
  "Persons/someguy/name" -&gt; Snow
  "Persons/otherperson/sex" -&gt; female
  </code></pre>
  
  <p>Preferably I have some nice recursive function where I traverse the XML code depth-first and simply stack all labels until I find a value and return that value together with the stack of labels. Unfortunately I am struggling to connect the return type with the input type as I return a Sequence of my input.. Let me show you what I have so far, clearly the foreach is a problem as this returns Unit, but the map would also not work as it returns a Seq.</p>
  
  <pre><code>def dfs(n: NodeSeq, keyStack: String, map: Map[String,String])
   :(NodeSeq, String, Map[String,String]) = {
    n.foreach(x =&gt; {
      if (x.child.isEmpty) {
        dfs(x.child, keyStack, map + (keyStack+ x.label + " " -&gt; x.text))
      }
      else {
        dfs(x.child, keyStack+ x.label + "/", map)
      }
    }
    )
  }
  </code></pre>
  
  <p>Would greatly appreciate the help!</p>