I often find myself adding an \@ operator to NodeSeq when I'm parsing xml, so I thought it might be generally useful enough to warrant adding to Scala core.
If you have an xml element defined:
scala> val x = <foo bar="apple" />
x: scala.xml.Elem = <foo bar="apple"></foo>
...the current code for getting the attribute string value looks like this:
scala> (x \ "@bar").text
res2: String = apple
The new operator removes the need for a pair of parenthesis and shortens the overall expression by 7 chars :
scala> x \@ "bar"
res3: String = apple
It's a pretty minor contribution, but makes my code look nicer at least. Is there anything I should do before submitting this as a pull-request?
best regards,
Roberto Tyley
--
// as an implicit
import xml.NodeSeq
object XmlSweetener {
class NodeSeqExtended(original: NodeSeq) {
def \@(attributeName: String): String = (original \ ("@" + attributeName)).text
}
implicit def nodseq2extended(x: NodeSeq) = new NodeSeqExtended(x)
}