Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 601573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:43:49+00:00 2026-05-13T16:43:49+00:00

I Have an XML Node that I want to add children to over time:

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:43:49+00:00Added an answer on May 13, 2026 at 4:43 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that looks like <?xml version='1.0' encoding='UTF-8'?> <root> <node name=foo1
I have an xml file that has an address node that looks like <address>
I have to read the xml node name from the following XML, but I
I have an XML document and want to insert a new node at a
I have the below fragement of XML, notice that the Reference node holds a
I have a Nokogiri xml node: node = <word n='ab' v='cd'>something</word> I want to
I have an XML file I'm trying to modify and want to add a
I have a XML created dynamically. However, I want to add a reference to
I have a function that builds and returns an XML document. I then want
I have an XML document that I want to generate unique IDs for. Some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.