如何使用Scala将节点与独特节点相邻?

pr1*_*001 5 xml xpath scala plist

我正在尝试解析Apple plist文件,我需要在其中获取一个数组Node.不幸的是,它唯一的唯一标识符就是它之前的同级节点<key>ProvisionedDevices</key>.现在我最好的想法是使用Java的XPATH查询或Node.indexOf.

这是一个例子:

<plist version="1.0">
       <dict>
               <key>ApplicationIdentifierPrefix</key>
               <array>
                       <string>RP8CBF4MRE</string>
               </array>
               <key>CreationDate</key>
               <date>2010-05-10T11:44:35Z</date>
               <key>DeveloperCertificates</key>
               <array>
               ...
               <key>ProvisionedDevices</key>
               <array>
               ... // I need the Nodes here
               </array>
       </dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

谢谢!

Dan*_*ral 5

这有效:

def findArray(key: Elem, xml: Elem) = {
  val components = xml \ "dict" \ "_"
  val index = components.zipWithIndex find (_._1 == key) map (_._2)
  index map (_ + 1) map components
}
Run Code Online (Sandbox Code Playgroud)


Ada*_*ung 5

  /**
   * Takes in plist key-value format and returns a Map[String, Seq[Node]]
   */
  def plistToMap(nodes:Seq[Node]) = {
    nodes.grouped(2).map {
      case Seq(keyNode, elementNode) => (keyNode.text, elementNode)
    }.toMap
  }
Run Code Online (Sandbox Code Playgroud)

然后你可以这样使用它:

println(plistToMap(xml \\ "dict" \ "_").get("ProvisionedDevices"))
Run Code Online (Sandbox Code Playgroud)