How can I get a Node next to a unique Node using Scala?

I am trying to parse an Apple plist file and I need to get the Node array inside it. Unfortunately, the only unique identifier is a sibling Node in front of him <key>ProvisionedDevices</key>. My best thoughts right now are to use the Java XPATH query 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>

Thank!

+5
source share
2 answers

It 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
}
+5
source
  /**
   * 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 as follows:

println(plistToMap(xml \\ "dict" \ "_").get("ProvisionedDevices"))
+5
source

All Articles