gpt4 book ai didi

xml - 如何使用 Scala 计算 XML 文件中的元素

转载 作者:数据小太阳 更新时间:2023-10-29 02:50:53 24 4
gpt4 key购买 nike

我在 scala 中使用 assert 函数来比较 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>

我希望能够计算 keydown 元素、keydown 元素、按键等的数量...

最佳答案

将你的录音标签做成一个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

编辑

要计算 recording 中的所有 XML 节点,保留 .filternot(...) 部分并使用 val allCount = children.size。即:

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

如果你想让它通用,我建议你通过按节点标签分组将它放在一个 Map 中。

例如,如果您只需要节点名称和大小,则可以这样做:

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

关于xml - 如何使用 Scala 计算 XML 文件中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52855610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com