Groovy throws error when "_-"are being used before root node while generating XML using XML markup builder .How to resolve it

While generating an XML from JSON I need to use "_-"before root node name due to naming convention rules but MarkupBuilder throws an error in groovy scripting. How can I resolve it?

input is JSON

{"x": "efgw343", "y": "Ywth"}

**THE CODE I used is **

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.JsonSlurper ;
import groovy.json.*

def Message processData(Message message) {

    def step = new JsonSlurper().parseText(message.getBody(java.lang.String))
def writer = new StringWriter()
def xmlRequest = new groovy.xml.MarkupBuilder(writer)
xmlRequest.mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
xmlRequest._-bbc(xmlns:"urn:xyz.com"){
           I_A(step.x)
           I_B(step.y)
}
message.setBody(writer.toString())
return message
}

the Output I am expecting is

<?xml version='1.0' encoding='utf-8'?>
<_-bbc xmlns='urn:xyz.com'>
  <I_A>efgw343</I_A>
  <I_B>Ywth</I_B>
</_-bbc>

THE ERROR MESSAGE I am getting is

groovy.lang.MissingPropertyException: No such property: _ for class: groovy.xml.MarkupBuilder
Possible solutions: mkp
    at groovyide_com.processData(groovyide_com:21)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at com.groovyide.ExecutorService$1.run(ExecutorService.java:144)
Error:
TypeError: Cannot convert undefined or null to object

Can anyone help in resolving this? Because this would reduce our code's length drastically.

Upvotes: 1

Views: 102

Answers (0)

Related Questions