I'm working with XML in Scala, parsing files that can reach ~20MB in a system with limited memory resources. I have to read the entire file and I must extract all the data from it. Being more concrete, the nodes I have to read have finite attributes and values.
I would like to know what is the best approach in terms of performance (or if both have the same performance). I ask this because I don't know how Scala handles its XML library and I may be missing some details.
1st approach
def firstApproach(root: Elem) =
for { n <- root \ "node" } yield handleNodeAttribute(n)
private def handleNodeAttribute(n: Node) = n match {
// here the possible cases -> type1, type2, type3
}
2nd approach
def secondApproach(root: Elem) = {
val nodes = root \ "node"
val type1 = filterNodesByAttribute(nodes, "attr", "type1")
// and so on -> type2, type3
}
private def filterNodesByAttribute(nodes: NodeSeq, attr: String, value: String) = {
nodes filter (node => (node \ ("@" + attr)) text == value)
}
Then, is there any advantage of using "\" and "\" methods over using pattern matching?
No comments:
Post a Comment