I Have an XML Node that I want to add children to over time:
val root: Node =
But I cannot see methods such as
In Scala xml nodes are immutable, but can do this:
var root =
def addToModel(child:Node) = {
root = root match {
case {children@ _*} => {children ++ child}
case other => other
}
}
addToModel(content )
It rewrites a new xml, by making a copy of the old one and adding your node as a child.
Edit: Brian provided more info and I figured a different to match.
To add a child to an arbitrary node in 2.8 you can do:
def add(n:Node,c:Node):Node = n match { case e:Elem => e.copy(child=e.child++c) }
That will return a new copy of parent node with the child added. Assuming you've stacked your children nodes as they became available:
scala> val stack = new Stack[Node]()
stack: scala.collection.mutable.Stack[scala.xml.Node] = Stack()
Once you've figured you're done with retrieving children, you can make a call on the parent to add all children in the stack like this:
stack.foldRight( :Node){(c:Node,n:Node) => add(n,c)}
I have no idea about the performance implication of using Stack and foldRight so depending on how many children you've stacked, you may have to tinker... Then you may need to call stack.clear too. Hopefully this takes care of the immutable nature of Node but also your process as you go need.