问题如下,想象一下没有任何特定模式的XML
<persons>
<total>2</total>
<someguy>
<firstname>john</firstname>
<name>Snow</name>
</someguy>
<otherperson>
<sex>female</sex>
</otherperson>
</persons>
对于处理我想在关键值映射中使用它:
"Persons/total" -> 2
"Persons/someguy/firstname" -> john
"Persons/someguy/name" -> Snow
"Persons/otherperson/sex" -> female
最好我有一些很好的递归函数,我遍历XML代码深度优先并简单地堆叠所有标签,直到找到一个值并将该值与标签堆栈一起返回。不幸的是,当我返回一个输入序列时,我正在努力将返回类型与输入类型连接起来。让我告诉你到目前为止我所拥有的内容,显然foreach是一个问题因为这会返回Unit,但地图也不会工作,因为它返回Seq。
def dfs(n: NodeSeq, keyStack: String, map: Map[String,String])
:(NodeSeq, String, Map[String,String]) = {
n.foreach(x => {
if (x.child.isEmpty) {
dfs(x.child, keyStack, map + (keyStack+ x.label + " " -> x.text))
}
else {
dfs(x.child, keyStack+ x.label + "/", map)
}
}
)
}
非常感谢帮助!
答案 0 :(得分:1)
经过一番游戏,这是我能做到的最优雅的方式。我不喜欢的是:
如果你有想法,请改进!
import scala.xml._
val xml = "<persons><total>2</total><someguy><firstname>john</firstname><name>Snow</name></someguy><otherperson><sex>female</sex></otherperson></persons>"
val result: Elem = scala.xml.XML.loadString(xml)
def linearize(node: Node, stack: String, map: Map[String,String])
: List[(Node, String, Map[String,String])] = {
(node, stack, map) :: node.child.flatMap {
case e: Elem => {
if (e.descendant.size == 1) linearize(e, stack, map ++ Map(stack + "/" + e.label -> e.text))
else linearize(e, stack + "/" + e.label, map)
}
case _ => Nil
}.toList
}
linearize(result, "", Map[String,String]()).flatMap(_._3).toMap
我们仍然需要在之后展平地图,但至少递归部分相当短。上面的代码应该适用于您的Scala工作表。
答案 1 :(得分:1)
受到Sparky的回答的启发,但即使对于更普遍的案例也适用:
val emptyMap = Map.empty[String,List[String]]
def xml2map(xml: String): Map[String,List[String]] = add2map(XML.loadString(xml), "", emptyMap)
private def add2map(node: Node, xPath: String, oldMap: Map[String,List[String]]): Map[String,List[String]] = {
val elems = node.child.filter(_.isInstanceOf[Elem])
val xCurr = xPath + "/" + node.label
val currElems = elems.filter(_.child.count(_.isInstanceOf[Elem]) == 0)
val nextElems = elems.diff(currElems)
val currMap = currElems.foldLeft(oldMap)((map, elem) => map + {
val key = xCurr + "/" + elem.label
val oldValue = map.getOrElse(key, List.empty[String])
val newValue = oldValue ::: List(elem.text)
key -> newValue
})
nextElems.foldLeft(currMap)((map, elem) => map ++ add2map(elem, xCurr, emptyMap))
}
对于XML
<persons>
<total>2</total>
<someguy>
<firstname>john</firstname>
<name>Snow</name>
<alive>in 1st season</alive>
<alive>in 2nd season</alive>
<alive>...</alive>
<alive>even in last season</alive>
<alive>how long more?</alive>
</someguy>
<otherperson>
<sex>female</sex>
</otherperson>
</persons>
它在下面生成一个Map [String,List [String]](在.toString()之后):
Map(
/persons/total -> List(2),
/persons/someguy/firstname -> List(john),
/persons/someguy/alive -> List(in 1st season, in 2nd season, ..., even in last season, how long more?),
/persons/otherperson/sex -> List(female),
/persons/someguy/name -> List(Snow)
)
答案 2 :(得分:0)
要考虑的一个方案是,元素具有前缀:
val xml = <a>
<b>
<c>1</c>
<d>2</d>
<e>
<z:f>3</z:f>
</e>
</b>
</a>
还有其他需要考虑的方案(包括实体,评论,声明),但这是一个很好的起点:
def nodeToMap(xml: Elem): Map[String, String] = {
def nodeToMapWithPrefix(prefix: String, xml: Node): Map[String, String] = {
val pathAndText = for {
child <- xml.child
} yield {
child match {
case e: Elem if e.prefix == null =>
nodeToMapWithPrefix(s"$prefix/${e.label}", e)
case e: Elem =>
nodeToMapWithPrefix(s"$prefix/${e.prefix}:${e.label}", e)
case t: Text => Map(prefix -> t.text)
case er: EntityRef => Map(prefix -> er.text)
}
}
pathAndText.foldLeft(Map.empty[String, String]){_ ++ _}
}
nodeToMapWithPrefix(xml.label, xml)
}
要考虑的另一个方案是文本不在叶元素处:
val xml = <a>
<b>text
<c>1</c>
<d>2</d>
</b>
</a>