Сделайте ваш тег записи в Seq[Node]
и подсчитайте каждый ярлык:
scala> :paste
// Entering paste mode (ctrl-D to finish)
val xml = <recording>
<mousemove y="53" x="300" t="9031"/>
<keydown kc="s" t="9759"/>
<keypress cc="s" t="9759"/>
<keyup kc="s" t="9829"/>
<execextern streamID="18" t="9833"/>
<keydown kc="s" t="10135"/>
<keypress cc="s" t="10135"/>
<keyup kc="s" t="10207"/>
<execextern streamID="19" t="10207"/>
<keydown kc="s" t="10934"/>
<keypress cc="s" t="10934"/>
<keyup kc="s" t="10989"/>
<execextern streamID="20" t="10989"/>
<keydown kc="s" t="11362"/>
<keypress cc="s" t="11366"/>
</recording>
// number of empty entries for whatever reason.
// Not necessary if you're not using children again after this.
val children = xml.child.filterNot(_.toString().trim.isEmpty)
val mousemoveCount = children.count(_.label == "mousemove")
val keydownCount = children.count(_.label == "keydown")
val keypressCount = children.count(_.label == "keypress")
val keyupCount = children.count(_.label == "keyup")
val execexternCount = children.count(_.label == "execextern")
println(s"number of mousemove events: $mousemoveCount")
println(s"number of keydown events: $keydownCount")
println(s"number of keypress events: $keypressCount")
println(s"number of keyup events: $keyupCount")
println(s"number of execextern events: $execexternCount")
// Exiting paste mode, now interpreting.
number of mousemove events: 1
number of keydown events: 4
number of keypress events: 4
number of keyup events: 3
number of execextern events: 3
РЕДАКТИРОВАТЬ
Чтобы подсчитать все узлы XML внутри recording
, оставьте часть .filternot(...)
и используйте val allCount = children.size
.IE:
val children = xml.child.filterNot(_.toString().trim.isEmpty)
val allCount = children.size
Кроме того, чтобы превратить это в общую функцию, вы можете просто превратить искомый дочерний узел в переменную:
scala> :paste
// Entering paste mode (ctrl-D to finish)
val xml = <recording>
<mousemove y="53" x="300" t="9031"/>
<keydown kc="s" t="9759"/>
<keypress cc="s" t="9759"/>
<keyup kc="s" t="9829"/>
<execextern streamID="18" t="9833"/>
<keydown kc="s" t="10135"/>
<keypress cc="s" t="10135"/>
<keyup kc="s" t="10207"/>
<execextern streamID="19" t="10207"/>
<keydown kc="s" t="10934"/>
<keypress cc="s" t="10934"/>
<keyup kc="s" t="10989"/>
<execextern streamID="20" t="10989"/>
<keydown kc="s" t="11362"/>
<keypress cc="s" t="11366"/>
</recording>
val children = xml.child.filterNot(_.toString().trim.isEmpty)
def countNodes(nodeName: String): Int = children.count(_.label == nodeName)
val allCount = children.size
println(s"number of mousemove events: ${countNodes("mousemove")}")
println(s"number of keydown events: ${countNodes("keydown")}")
println(s"number of keypress events: ${countNodes("keypress")}")
println(s"number of keyup events: ${countNodes("keyup")}")
println(s"number of execextern events: ${countNodes("execextern")}")
println(s"total number of events: $allCount")
// Exiting paste mode, now interpreting.
number of mousemove events: 1
number of keydown events: 4
number of keypress events: 4
number of keyup events: 3
number of execextern events: 3
total number of events: 15
РЕДАКТИРОВАТЬ 2
Если вы хотите сделать этот универсальный, я рекомендую поместить его в карту путем группировки по метке узла.
Например, если вы просто хотите узелимена и размеры, что-то вроде этого будет делать:
children.groupBy(_.label).map {
case(k, v) => (k, v.size)
}
// Map(mousemove -> 1, keydown -> 4, execextern -> 3, keypress -> 4, keyup -> 3)
Если вы хотите целые узлы, вы можете просто удалить .map
:
import scala.xml.Node
val nodeSizeMap: Map[String, Seq[Node]] = children.groupBy(_.label)
// Map(
// mousemove -> ArrayBuffer(<mousemove y="53" x="300" t="9031"/>),
// keydown -> ArrayBuffer(<keydown kc="s" t="9759"/>, <keydown kc="s" t="10135"/>, <keydown kc="s" t="10934"/>, <keydown kc="s" t="11362"/>),
// execextern -> ArrayBuffer(<execextern streamID="18" t="9833"/>, <execextern streamID="19" t="10207"/>, <execextern streamID="20" t="10989"/>),
// keypress -> ArrayBuffer(<keypress cc="s" t="9759"/>, <keypress cc="s" t="10135"/>, <keypress cc="s" t="10934"/>, <keypress cc="s" t="11366"/>),
// keyup -> ArrayBuffer(<keyup kc="s" t="9829"/>, <keyup kc="s" t="10207"/>, <keyup kc="s" t="10989"/>)
// )
В контексте:
scala> :paste
// Entering paste mode (ctrl-D to finish)
val xml = <recording>
<mousemove y="53" x="300" t="9031"/>
<keydown kc="s" t="9759"/>
<keypress cc="s" t="9759"/>
<keyup kc="s" t="9829"/>
<execextern streamID="18" t="9833"/>
<keydown kc="s" t="10135"/>
<keypress cc="s" t="10135"/>
<keyup kc="s" t="10207"/>
<execextern streamID="19" t="10207"/>
<keydown kc="s" t="10934"/>
<keypress cc="s" t="10934"/>
<keyup kc="s" t="10989"/>
<execextern streamID="20" t="10989"/>
<keydown kc="s" t="11362"/>
<keypress cc="s" t="11366"/>
</recording>
val children = xml.child.filterNot(_.toString().trim.isEmpty)
def countNodes(nodeName: String): Int = children.count(_.label == nodeName)
val allCount = children.size
// if you just want to print
children.groupBy(_.label).foreach {
case (k, v) => println(s"number of $k events: ${v.size}")
}
println()
// if you want to do something with the values
val nodeSizeMap: Map[String, Int] = children.groupBy(_.label).map {
case(k, v) => (k, v.size)
}
// ... do something with nodeSizeMap
nodeSizeMap.foreach {
case (k, v) => println(s"number of $k events: $v")
}
// Exiting paste mode, now interpreting.
number of mousemove events: 1
number of keydown events: 4
number of execextern events: 3
number of keypress events: 4
number of keyup events: 3
number of mousemove events: 1
number of keydown events: 4
number of execextern events: 3
number of keypress events: 4
number of keyup events: 3
РЕДАКТИРОВАТЬ 3
Чтобы сделать это еще более универсальным и разрешить поиск по вложенным тегам, вы можете искать по волшебному XMLподстановочный знак _
.Вот пример (извините за глупость XML):
scala> :paste
// Entering paste mode (ctrl-D to finish)
val xml = <family>
<mother name="julie" />
<father name="harold" />
<child name="billy" status="good child" />
<child name="charlie" status="good child" />
<child name="mandy" status="bad child" />
<child name="nigel" status="bad child" />
<extendedfamily>
<uncle name="jeff" />
<auntie name="vicky" />
<cousin name="little boy 1" />
<cousin name="little boy 2" />
</extendedfamily>
</family>
val familyMap = (xml \\ "_").groupBy(_.label).map { case (k, v) => (k, v.size) }
familyMap foreach {
case (k, v) => println(s"$k count: $v")
}
// Exiting paste mode, now interpreting.
mother count: 1
auntie count: 1
uncle count: 1
child count: 4
extendedfamily count: 1
father count: 1
cousin count: 2
family count: 1