0

本主题相关

问题如下,想象一个没有任何特定模式的 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)
    }
  }
  )
}

非常感谢您的帮助!

4

3 回答 3

1

在玩了一些之后,这是我能做到的最优雅的方式。我不喜欢的是:

  • 对于每个孩子来说,它都是深度优先的,所以你需要在之后把结果弄平。这也是我错过根节点标签的原因。
  • 它沿途拖了很多 XML,所以它可能太占用内存?

如果您有想法,请改进!

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 工作表中使用。

于 2018-02-13T17:45:01.853 回答
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)
)
于 2018-04-16T12:15:51.160 回答
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>
于 2018-02-19T06:02:46.510 回答