Home > database >  Filter XML nodes having specific values in groovy
Filter XML nodes having specific values in groovy

Time:10-07

My requirement is to find all the uname from input xml where action = eSign.

To begin with, I have tried the below code, to filter the parent nodes where action does not contain eSign that is != eSign

However the below code removes all the nodes and does not return any value when I add the != operator and when I replace the != with == 'eSign' the code returns the nodes with eSign. Not able to understand what is wrong when I use != operator for the code.

    import com.sap.gateway.ip.core.customdev.util.Message;
    import java.util.HashMap;
    import groovy.xml.XmlUtil;
    import groovy.xml.StreamingMarkupBuilder;
    import groovy.xml.*;
    
    def Message processData(Message message) 
{        
//Body       
def parsed = new XmlParser().parseText(message.body)      parsed.data.'**'.findAll { it.action.text() != 'eSign' }.each { it.parent().remove(it) }     
message.body = XmlUtil.serialize(parsed)      
return message;    
 }

Input xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DataPageRestResult>
    <responseStatus>SUCCESS</responseStatus>
    <response>
        <offset>0</offset>
        <limit>200</limit>
        <object>
            <name>doc_audit</name>
        </object>
    </response>
<data>
        <data>
            <id>40982</id>
            <timestamp>2021-09-23T10:32:28Z</timestamp>
            <uname>[email protected]</uname>
            <action>GetDocVersion</action>   
        </data>
 <data>
            <id>20841</id>
            <timestamp>2021-08-02T21:13:08Z</timestamp>
            <uname>[email protected]</uname>
            <action>eSign</action>
        </data>
 <data>
            <id>20829</id>
            <timestamp>2021-08-02T21:11:42Z</timestamp>
            <uname>[email protected]</uname>
            <action>MDWTaskCompletion</action>         
        </data>
 <data>
            <id>20834</id>
            <timestamp>2021-08-02T21:12:25Z</timestamp>
            <uname>[email protected]</uname>
            <action>eSign</action>       
        </data>
 <data>
            <id>20829</id>
            <timestamp>2021-08-02T21:11:42Z</timestamp>
            <uname>[email protected]</uname>
            <action>MDWTaskCompletion</action>       
        </data>
      <data>
            <id>20830</id>
            <timestamp>2021-08-02T21:11:42Z</timestamp>
            <uname>[email protected]</uname>
            <action>eSign</action>        
        </data>
    </data>
</DataPageRestResult>

CodePudding user response:

You should be able to do something like this:

Message processData(Message message) {
    def parsed = new XmlParser().parseText(message.body)
    parsed.data.'**'.findAll { it.action.text() == 'eSign' }.each { it.parent().remove(it) }
    message.body = XmlUtil.serialize(parsed)
    message
}

CodePudding user response:

Consider coercing the "data" Node into a Collection to take advatage of Collection#removeAll.

import groovy.xml.XmlUtil

class Example {

    public static class Message {
        String body
    }
    
    private static final XmlParser XML_PARSER = new XmlParser()
    private static final Message EXAMPLE_MESSAGE = new Message(body: '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?><DataPageRestResult><responseStatus>SUCCESS</responseStatus><response><offset>0</offset><limit>200</limit><object><name>doc_audit</name></object></response><data><data><id>40982</id><timestamp>2021-09-23T10:32:28Z</timestamp><uname>[email protected]</uname><action>GetDocVersion</action></data><data><id>20841</id><timestamp>2021-08-02T21:13:08Z</timestamp><uname>[email protected]</uname><action>eSign</action></data><data><id>20829</id><timestamp>2021-08-02T21:11:42Z</timestamp><uname>[email protected]</uname><action>MDWTaskCompletion</action></data><data><id>20834</id><timestamp>2021-08-02T21:12:25Z</timestamp><uname>[email protected]</uname><action>eSign</action></data><data><id>20829</id><timestamp>2021-08-02T21:11:42Z</timestamp><uname>[email protected]</uname><action>MDWTaskCompletion</action></data><data><id>20830</id><timestamp>2021-08-02T21:11:42Z</timestamp><uname>[email protected]</uname><action>eSign</action></data></data></DataPageRestResult>''')
    
    public static Message processData(Message message) {
        def parsed = XML_PARSER.parseText(message.body)
        (parsed.'data'[0] as Collection)
            .removeAll { it."action".text() != "eSign" }
        message.body = XmlUtil.serialize(parsed)
        return message
    }
    
    public static void main(String[] args) {       
        println processData(EXAMPLE_MESSAGE).body
    }
}    
  • Related