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 the usual Scala fashion, all Node, Elem, etc. instances are immutable. You can work it the other way around:
scala> val child = <child>foo</child>
child: scala.xml.Elem = <child>foo</child>
scala> val root = <root>{child}</root>
root: scala.xml.Elem = <root><child>foo</child></root>
See http://sites.google.com/site/burakemir/scalaxbook.docbk.html for more information.
Since scala 2.10.0 the instance constructor of Elem has changed, if you want use naive solution written by @Daniel C. Sobral, it should be:
xmlSrc match {
case xml.Elem(prefix, label, attribs, scope, child @ _*) =>
xml.Elem(prefix, label, attribs, scope, child.isEmpty, child ++ ballot : _*)
case _ => throw new RuntimeException
}
For me, it works very good.
Since XML
are immutable
, you have to create a new one each time you want to append a node, you can use Pattern matching
to add your new node:
var root: Node = <model></model>
def addToModel(newNode: Node) = root match {
//match all the node from your model
// and make a new one, appending old nodes and the new one
case <model>{oldNodes@_*}</model> => root = <model>{oldNodes}{newNode}</model>
}
addToModel(<subsection>content</subsection>)
Well start with this:
def addChild(n: Node, newChild: Node) = n match {
case Elem(prefix, label, attribs, scope, child @ _*) =>
Elem(prefix, label, attribs, scope, child ++ newChild : _*)
case _ => error("Can only add children to elements!")
}
The method ++
works here because child
is a Seq[Node]
, and newChild
is a Node
, which extends NodeSeq
, which extends Seq[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 Elem
object, as well as creating a new Seq
of 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:
class AddChildrenTo(label: String, newChild: Node) extends RewriteRule {
override def transform(n: Node) = n match {
case n @ Elem(_, `label`, _, _, _*) => addChild(n, newChild)
case other => other
}
}
Then, use it like this:
val newXML = new RuleTransformer(new AddChildrenTo(parentName, newChild)).transform(oldXML).head
On Scala 2.7, replace head
with first
.
Example on Scala 2.7:
scala> val oldXML = <root><parent/></root>
oldXML: scala.xml.Elem = <root><parent></parent></root>
scala> val parentName = "parent"
parentName: java.lang.String = parent
scala> val newChild = <child/>
newChild: scala.xml.Elem = <child></child>
scala> val newXML = new RuleTransformer(new AddChildrenTo(parentName, newChild)).transform(oldXML).first
newXML: scala.xml.Node = <root><parent><child></child></parent></root>
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 against books
, which would then get its child
(which really should have been named children
), find the nth book
in 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.
Scales Xml allows for simple in place changes via folding over XPaths, adding in children to a particular sub node fits right into this approach.
See In-Place Transformations for more details.
I implement my 'appendChild' method in the following way:
def appendChild(elem: Node, child: Node, names: String) = {
appendChild(elem, child, names.split("/"))
}
private def appendChild(elem: Node, child: Node, names: Array[String]) = {
var seq = elem.child.diff(elem \ names.head)
if (names.length == 1)
for (re <- elem \ names.head)
seq = seq ++ re.asInstanceOf[Elem].copy(child = re.child ++ child)
else
for (subElem <- elem \ names.head)
seq = seq ++ appendChild(subElem, child, names.tail)
elem.asInstanceOf[Elem].copy(child = seq)
}
The method appends children to your nodes in recursive manner. In the 'if' statement it simply calls 'copy' method of Elem class to produce new instances of affected children (those may be plural). Then in 'else' statement recursive calls to 'appendChild' method verify resulting XML will be rebuilt. Before 'if-else' there are sequence which is built from non-affected children. At the end, we need to copy this sequence to origin element.
val baz = <a><z x="1"/><b><z x="2"/><c><z x="3"/></c><z x="4"/></b></a>
println("Before: \n" + XmlPrettyPrinter.format(baz.toString()))
val res = appendChild(baz, <y x="5"/>, "b/c/z")
println("After: \n" + XmlPrettyPrinter.format(res.toString()))
Results:
Before:
<a>
<z x="1"/>
<b>
<z x="2"/>
<c>
<z x="3"/>
</c>
<z x="4"/>
</b>
</a>
After:
<a>
<z x="1"/>
<b>
<z x="2"/>
<z x="4"/>
<c>
<z x="3">
<y x="5"/>
</z>
</c>
</b>
</a>