How can I get a Node adjacent to a unique Node using Scala?
Asked Answered
P

2

5

I'm trying to parse an Apple plist file and I need to get an array Node within it. Unfortunately its only unique identifier is sibling Node right before it, <key>ProvisionedDevices</key>. Right now my best thoughts are to use Java's XPATH querying or Node.indexOf.

Here is an example:

<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>

Thanks!

Paramo answered 10/5, 2010 at 14:35 Comment(0)
P
5

This works:

def findArray(key: Elem, xml: Elem) = {
  val components = xml \ "dict" \ "_"
  val index = components.zipWithIndex find (_._1 == key) map (_._2)
  index map (_ + 1) map components
}
Pliner answered 10/5, 2010 at 15:30 Comment(2)
zipWithIndex doesn't seem to be a member of NodeSeq. Was it added in 2.8? I'm using 2.7.7.Paramo
@Paramo Put a .toList before it, then.Pliner
Q
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
  }

Then you can use it this way:

println(plistToMap(xml \\ "dict" \ "_").get("ProvisionedDevices"))
Queri answered 10/5, 2010 at 16:48 Comment(1)
Thanks, Adam, but where do you get the grouped method? It is not a method of Seq nor NodeSeq. I'm using Scala 2.7.7.Paramo

© 2022 - 2024 — McMap. All rights reserved.