> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Adding Missing Element to Child Element in XML Body

> Adding missing element to child element in XML body with Groovy script

## Groovy Script

```groovy theme={null}
import groovy.xml.XmlSlurper
import groovy.xml.XmlUtil

// XML is parsed
def parsedXml = new XmlSlurper().parseText(requestBodyTextFromClient)

// First element in Body is found
def methodNode = parsedXml.'**'.find { it.parent().name() == 'Body' }

// Check if <ELEMENT_NAME> element exists in the children element of body
def <ELEMENT_NAME>Node = methodNode.children().find { it.name() == '<ELEMENT_NAME>' }

// If <ELEMENT_NAME> element does not exist, it is added to the child element within body
if (!<ELEMENT_NAME>) {
  methodNode.appendNode {
    <ELEMENT_NAME>(' ')
  }
}

// XML is serialized.
requestBodyTextToTargetAPI = XmlUtil.serialize(parsedXml)
```

## Explanation

This script performs the following operations:

1. **XML Parse**: XML data coming from request body is parsed
2. **Body Element Finding**: First element in Body is found
3. **Element Check**: Checks if the specified element exists
4. **Element Addition**: If element does not exist, it is added to the child element within body
5. **XML Serialize**: Updated XML is serialized

<Note>
  This script should be run on the request line (Request Policy) because it uses the `requestBodyTextFromClient` and `requestBodyTextToTargetAPI` variables. You need to write the actual element name instead of `<ELEMENT_NAME>`.
</Note>
