troubleshooting Question

Drop XML records based on items in an array using Groovy

Avatar of kalyangkm
kalyangkmFlag for United States of America asked on
XML* Groovy
12 Comments1 Solution14 ViewsLast Modified:

Hi Folks,


I have an XML with a bunch of records with UserID being the unique value in all. And also have an array of UserIDs. Using Groovy I need to retain only those records in the XML which exists in the

userArray = ["885247","1149285"]

Open in new window

 I have a groovy which only brings me one record. I am not good at groovy with XML parsers please suggest how to make it work properly


XML

<EmpJob>
    <EmpJob>
      <managerId>1130351</managerId>       <userId>1158278</userId>       <managerUserNav>         <User>           <defaultFullName>Roy coon</defaultFullName>         </User>       </managerUserNav>     </EmpJob>     <EmpJob>       <managerId>874274</managerId>       <userId>4072314</userId>       <managerUserNav>         <User>           <defaultFullName>Roman Card</defaultFullName>         </User>       </managerUserNav>     </EmpJob>     <EmpJob>       <managerId>73661</managerId>       <userId>42479</userId>       <managerUserNav>         <User>           <defaultFullName>Pawan Kalyan</defaultFullName>         </User>       </managerUserNav>     </EmpJob>     <EmpJob>       <managerId>NO_MANAGER</managerId>       <managerUserNav/>       <userId>1149285</userId>     </EmpJob>     <EmpJob>       <managerId>794895</managerId>       <userId>885189</userId>       <managerUserNav>         <User>           <defaultFullName>BEVERLY Hills</defaultFullName>         </User>       </managerUserNav>     </EmpJob>     <EmpJob>       <managerId>476232</managerId>       <userId>885247</userId>       <managerUserNav>         <User>           <defaultFullName>Bruno Abdulla</defaultFullName>         </User>       </managerUserNav>     </EmpJob>    </EmpJob>

Open in new window


My groovy:

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 body = message.getBody();       def list=new XmlParser().parse(body);      // def userList = message.getProperty("userIDs");       String[] userArray = ["885247","1149285"];       def newPayload;              userArray.eachWithIndex { id ->                def newNode = list.EmpJob.findAll{ it.userId.text() == id }       newPayload.appendNode(newNode);                         }              message.setBody(XmlUtil.serialize(newPayload));       return message; }

Open in new window


I need the following XML output which I am not able to achieve with the above groovy


<EmpJob>
    <EmpJob>
      <managerId>NO_MANAGER</managerId>
      <managerUserNav/>
      <userId>1149285</userId>
    </EmpJob>
    <EmpJob>
      <managerId>476232</managerId>
      <userId>885247</userId>
      <managerUserNav>
        <User>
          <defaultFullName>Bruno Abdulla</defaultFullName>
        </User>
      </managerUserNav>
    </EmpJob>
</EmpJob>

Open in new window

ASKER CERTIFIED SOLUTION
Gertone (Geert Bormans)
Information Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2006

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 12 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 12 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004