Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala XML Building: Adding children to existing Nodes

Tags:

xml

scala

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?

like image 842
BefittingTheorem Avatar asked Feb 04 '10 10:02

BefittingTheorem


4 Answers

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 nthbook 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.

like image 151
Daniel C. Sobral Avatar answered Nov 03 '22 05:11

Daniel C. Sobral


In Scala xml nodes are immutable, but can do this:

var root = <model/>

def addToModel(child:Node) = {
  root = root match {
    case <model>{children@ _*}</model> => <model>{children ++ child}</model>
    case other => other
  }
}

addToModel(<subsection>content</subsection>)

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(<parent/>: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.

like image 9
huynhjl Avatar answered Nov 03 '22 05:11

huynhjl


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.

like image 6
Murdix Avatar answered Nov 03 '22 03:11

Murdix


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>)
like image 3
Patrick Avatar answered Nov 03 '22 04:11

Patrick