开发者

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

开发者 https://www.devze.com 2022-12-30 06:10 出处:网络
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>.

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>
            开发者_如何学Python   <date>2010-05-10T11:44:35Z</date>
               <key>DeveloperCertificates</key>
               <array>
               ...
               <key>ProvisionedDevices</key>
               <array>
               ... // I need the Nodes here
               </array>
       </dict>
</plist>

Thanks!


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
}


  /**
   * 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"))
0

精彩评论

暂无评论...
验证码 换一张
取 消