Find all nodes that have an attribute that matches a certain value with scala

前端 未结 5 1147
遥遥无期
遥遥无期 2021-02-08 04:00

I saw the following example on Nabble, where the goal was to return all nodes that contain an attribute with an id of X that contains a value Y:

//find all nodes         


        
5条回答
  •  广开言路
    2021-02-08 04:47

    I'm quite new to Scala, I propose you this solution, but I'm not sure this is the best one:

    def attributeValueEquals(value: String)(node: Node) = {
      node.attributes.foldLeft(false)((a : Boolean, x : MetaData) => a | (x.value == value))
    }
    
    val testResults = (xml \\ "_").filter(attributeValueEquals("test")) 
    println("testResults: " + testResults )
    
    // prints: testResults: ArrayBuffer(hello, 
    // 

    hello

    , // )

提交回复
热议问题