I Have an XML Node that I want to add children to over time:
val root: Node = <model></model>
But I cannot see methods such as addChild(), as I would like to write something along the lines of:
def addToModel() = {
root.addChild(<subsection>content</subsection>)
}
So after a single call to this method the root xml would be:
<model><subsection>content</subsection></model>
The only class I can see that has the ability to append a Node is the NodeBuffer. Am I missing something fundamental here?
Well start with this:
The method
++works here becausechildis aSeq[Node], andnewChildis aNode, which extendsNodeSeq, which extendsSeq[Node].Now, this doesn’t change anything, because XML in Scala is immutable. It will produce a new node, with the required changes. The only cost is that of creating a new
Elemobject, as well as creating a newSeqof children. The children node, themselves, are not copied, just referred to, which doesn’t cause problems because they are immutable.However, if you are adding children to a node way down on the XML hierarchy, things get complicated. One way would be to use zippers, such as described in this blog.
You can, however, use
scala.xml.transform, with a rule that will change a specific node to add the new child. First, write a new transformer class:Then, use it like this:
On Scala 2.7, replace
headwithfirst.Example on Scala 2.7:
You could make it more complex to get the right element, if just the parent isn’t enough. However, if you need to add the child to a parent with a common name of a specific index, then you probably need to go the way of zippers.
For instance, if you have
<books><book/><book/></books>, and you want to add<author/>to the second, that would be difficult to do with rule transformer. You’d need a RewriteRule againstbooks, which would then get itschild(which really should have been namedchildren), find the nthbookin them, add the new child to that, and then recompose the children and build the new node. Doable, but zippers might be easier if you have to do that too much.