ERPClimb logoERPClimb
Groovy Scripting Deep Dive
Integration Suite & Cloud Integration (CPI)advanced 5 min read

CPI Groovy Part 2: Processing XML and JSON Efficiently

Preview · a free account unlocks the full lesson

Key takeaway

Parse and build payloads in Groovy: XmlSlurper versus XmlParser, MarkupBuilder and StreamingMarkupBuilder, JsonSlurper and JsonBuilder, namespace handling, and memory-aware techniques for large documents.

Visual overview

CPI Groovy Part 2: Processing XML and JSON Efficiently — learning sheetFlow: Choose parser → Navigate with GPath → Handle namespaces → Build output with builders → Stream large payloads. Key ideas: READING XML; WRITING XML; JSON; NAMESPACES. Watch out: String concatenation to build XML produces invalid documents when data contains special characters.ERPClimb.com / CPI / LESSON 2/15 / LEARNING SHEETCPI Groovy Part 2: Processing XML and JSON Efficiently1 · HOW IT FLOWS1Chooseparser2Navigatewith GPath3Handlenamespaces4Buildoutput withbuilders5Streamlargepayloads2 · KEY IDEAS EXPLAINEDReading XMLXmlSlurper parses lazily and lets you navigatewith GPath expressions likeroot.Order.Item.findAll { it.Qty.toInteger() >…Writing XMLMarkupBuilder and StreamingMarkupBuilder createXML with proper escaping, so special characterslike & and < in data don't break the…JSONJsonSlurper parses JSON into maps and lists;JsonOutput.toJson or JsonBuilder creates JSON.Many APIs return JSON, and iFlows often convert…NamespacesXML from SOAP services and IDoc-like structuresoften uses namespaces. With XmlSlurper, eitherdeclare namespaces with declareNamespace and use…3 · REMEMBER · WATCH OUTRememberXmlSlurper is lazy and memory-friendly for reading; XmlParser builds afull mutable tree.StreamingMarkupBuilder writes large XML without holding the wholedocument.JsonSlurper and JsonOutput/JsonBuilder handle JSON parsing andcreation.!Watch outString concatenation to build XMLproduces invalid documents when datacontains special characters.4 · IN A REAL PROJECTAn iFlow built XML by string concatenation and failed whenever a customer name contained an ampersand,producing invalid XML that receivers rejected. Rewriting the output with MarkupBuilder fixed the escaping andremoved a recurring weekly incident.Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Part 1 of this series covered the Message API; Part 2 is about working with payload formats in Groovy. XmlSlurper parses XML lazily and is memory-friendly for reading, letting you navigate with GPath expressions such as root.Order.Item.findAll { it.Qty.toInteger() > 0 }. It's a good fit for reading and filtering. XmlParser, by contrast, builds a full mutable node tree that's easier to modify but uses considerably more memory — the choice between them comes down to whether you need to read the structure or actually change it.

For writing XML, MarkupBuilder and StreamingMarkupBuilder produce well-formed output with proper escaping, so special characters in the data — an ampersand or a less-than sign in a customer name, for instance — don't break the resulting document the way naive string concatenation would. For genuinely large outputs, StreamingMarkupBuilder writes directly to a stream instead of building the entire document in memory first.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.