Himanshu Yadav
Himanshu Yadav

Reputation: 13587

Groovy: Create a new node

I am building the parent xml by reading it from a request xml Here is the code:

def createXML(requestXML)
{
  def formXML = new XmlSlurper().parseText(requestXML)
  def formMap = [:]
  formXML.children.each {
   formMap[it.name()] = it
  }
  buildComponentXML(formMap)

}

def buildComponentXML(formMap)
{
  def compXML = formMap['componentName']
  def anotherNode = new Node (compXML, 'AnotherNode',1234555)
  println XmlUtil.serialize(compXML)
}

Exception

Could not find matching constructor for: groovy.util.Node(groovy.util.slurpersupport.NodeChild, java.lang.String, java.lang.String)

Upvotes: 0

Views: 1485

Answers (1)

tim_yates
tim_yates

Reputation: 171084

It's hard to say, but what I think you want is:

def buildComponentXML( formMap ) {
  def compXML = formMap['componentName']
  compXML.appendNode { AnotherNode( 1234555 ) }
  println XmlUtil.serialize(compXML)
}

But then, that's the same as:

def createXML( requestXML ) {
  def formXML = new XmlSlurper().parseText(requestXML)
  buildComponentXML(formXML)
}

def buildComponentXML( formXML ) {
  formXML.'componentName'.appendNode { AnotherNode( 1234555 ) }
  println XmlUtil.serialize(formXML)
}

As sticking things into a map is kinda duplicating the effort of XmlSlurper (assuming I have the correct end of the stick wrt what you're trying to do)

Upvotes: 1

Related Questions