cpi groovy parser supports only few name text and nodelist, Possible solutions: text(), text(), wait(), list(), name(), last()
<?xml version="1.0"?>
<root>
<person id="1">
<name>John</name>
<age>30</age>
</person>
<person id="2">
<name>Jane</name>
<age>25</age>
</person>
</root>
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.util.XmlParser
import java.util.HashMap;
import java.util.*;
def Message processData(Message message) {
try {
def body = message.getBody()
def xml = new XmlParser().parse(body);
// Check if the root node exists
if (xml instanceof groovy.util.Node) {
println("Root node: ${xml.name()}")
// Iterate through child nodes
xml.depthFirst().each { node ->
if (node instanceof groovy.util.Node) {
println("Node name: ${node.name()}")
//pdp test
if(node.name()=="age" && node.text()=="25")
{ println("----pdp testing-------")
println("Node name age with value 25: ${node.name()}")}
if(node.name()=="age" && node.parent().name()=="person")
{ println("----pdp testing-------")
println("Node name age and parent with person: ${node.name()}")
}
//pdp test end
if(node.parent()=="person" && node.text()=="25") println("Node parent person with value 25: ${node.name()}") // Check for attributes
node.attributes().each { attr ->
println("Attribute '${attr.key}' exists with value: ${attr.value}")
}
// Check for elements
println("Element text: ${node.text()}")
}
}
} else {
println("Root node does not exist.")
}
// Fetch properties of the node
//def targetNode = xml.'**'.find { node ->
// node.name() == 'age' && node.parent().@id.text() == '1'
//}
// Fetch parent node name (with a check for null parent)
//def parentNodeName = targetNode.parent() ? targetNode.parent().name() : "N/A"
// Fetch child node names
//def childNodeNames = targetNode.parent().children().collect { it.name() }
//
// Fetch sibling nodes
//def siblingNodes = targetNode.parent().parent().person.findAll { it.@id.text() != '1' }
//
// Fetch preceding nodes by iterating through the parent's children
//def precedingNodes = targetNode.parent().parent().person.takeWhile { it != targetNode.parent() }
// Fetch next immediate sibling by iterating through the parent's children
//def nextSibling = targetNode.parent().parent().person.find { it == targetNode.parent() }?.next()
// Fetch ancestors
//def ancestors = targetNode.parent().ancestors()
// Check if value contains a specific substring
//def containsSubstring = extractedValue.contains('3')
// Check if value is not null
//def isNotNull = extractedValue != null
// Print the fetched properties and check results
//println("Parent Node Name: $parentNodeName")
/*println("Child Node Names: $childNodeNames")
println("Sibling Nodes: ${siblingNodes*.name().join(', ')}")
println("Preceding Nodes: ${precedingNodes*.name().join(', ')}")
println("Next Sibling: ${nextSibling?.name()}")
println("Ancestors: ${ancestors*.name().join(', ')}")
println("Value Contains '3': $containsSubstring")
println("Value is Not Null: $isNotNull")*/
} catch (Exception e) {
println("Error processing XML: ${e.message}")
}
return message
}