Simbu
Simbu

Reputation: 796

Building xml and merging CDATA xml using groovy

I have mule flow which stores the xml in session variable. I want this xml to be inserted in the xml which is generated in groovy.

My session variable looks like #[sessionVars.shippingdetails]

This session variable has <a><Subject1>science</Subject1><Subject2>Maths</Subject2></a>

When i use session variable in my xmlmarkup builder as like below. I am getting error as groovy.lang.GroovyRuntimeException: Namespace prefix: CDATA is not bound to a URI (javax.script.ScriptException). Message payload is of type: CaseInsensitiveHashMap (org.mule.api.transformer.TransformerMessagingException). Message payload is of type: CaseInsensitiveHashMap

import groovy.xml.XmlUtil
def builder = new groovy.xml.StreamingMarkupBuilder()
builder.encoding = "UTF-8"

// MAPPING
def person = {
  // use mkp object which provides features like setting the namespace
  mkp.xmlDeclaration()
  mkp.declareNamespace("":"urn:abc:alos:BaseComponents")

  //start with the XML root node closure
  ItemRequest {
    Requester{
        subject(message.payload.subject)
    }
  Item{
    Title(message.payload.name)
    Description(message.payload.desc)
    Category {
        CategoryID(message.payload.cat_id)
    }
    ConditionID (message.payload.condition)
    Mark (message.payload.Mark)

 ShippingDetails [CDATA[sessionVars.shippingdetails]]

    }
  }
}

// WRITING
def writer = new StringWriter()
writer << builder.bind(person)
println writer.toString()

XmlUtil.serialize(builder.bind(person))

Hence my output xml should like below.

<?xml version="1.0" encoding="UTF-8"?>
<ItemRequest xmlns="urn:abc:alos:BaseComponents">
    <Requester>
        <subject>something</subject>
    </Requester>
    <Item>
        <Title>Cooler Can Blue</Title>
        <Description>This is the place for description.</Description>
        <Category>
            <CategoryID>562</CategoryID>
        </Category>
        <ConditionID>25</ConditionID>
        <Mark>3</Mark>
        <ShippingDetails>
            <a>
                <Subject1>science</Subject1>
                <Subject2>Maths</Subject2>
            </a>
        </ShippingDetails>
    </Item>
</ItemRequest>

Upvotes: 0

Views: 366

Answers (1)

Michael Easter
Michael Easter

Reputation: 24468

Using Groovy 2.4.3, here is one way to get the output specified. (This does not address Mule):

import groovy.xml.*

def markupBuilder = new StreamingMarkupBuilder()

def xml = markupBuilder.bind { builder ->
    mkp.declareNamespace( "": "urn:abc:alos:BaseComponents" )
    ItemRequest {
        Requester {
            subject("something")
        }
        Item {
            Title("Cooler Can Blue")
            Description("This is the place for description.")
            Category {
                CategoryID("562")
            } 
            ConditionID("25")
            Mark("3")
            ShippingDetails {
                a {
                    Subject1("science")
                    Subject2("Maths")
                }
            }
        }
    }
}
def goal = """<?xml version="1.0" encoding="UTF-8"?><ItemRequest xmlns="urn:abc:alos:BaseComponents">
  <Requester>
    <subject>something</subject>
  </Requester>
  <Item>
    <Title>Cooler Can Blue</Title>
    <Description>This is the place for description.</Description>
    <Category>
      <CategoryID>562</CategoryID>
    </Category>
    <ConditionID>25</ConditionID>
    <Mark>3</Mark>
    <ShippingDetails>
      <a>
        <Subject1>science</Subject1>
        <Subject2>Maths</Subject2>
      </a>
    </ShippingDetails>
  </Item>
</ItemRequest>
"""

assert goal == XmlUtil.serialize(xml)

Upvotes: 0

Related Questions