Groovy:创建一个新节点

时间:2013-09-16 12:36:46

标签: xml groovy

我正在通过从请求xml中读取它来构建父xml。这是代码:

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)
}

异常

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

1 个答案:

答案 0 :(得分:1)

很难说,但我想要你想要的是:

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

但是,那就是:

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

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

因为将东西粘贴到地图中有点复制了XmlSlurper的工作(假设我有正确的结束了你正在尝试做的事情)