Forum Discussion

Santhosh_P's avatar
Santhosh_P
Contributor
8 years ago
Solved

How do i remove a node from an xml?

<HomeOwnersContext xmlns="http://schemas.datacontract.org/2004/07/Homesite.Homeowners.ContextModel.Api.Context" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <DisplayOnlyData/>
<QuoteData>
<animalsBitten>false</animalsBitten>
<burglarAlarm>No</burglarAlarm>
<businessOnPremises>false</businessOnPremises>
<totalNumberOfPeople>2</totalNumberOfPeople>
</QuoteData>
<QuoteHeader>
<Channel>Direct_Web</Channel>
<FormCode>3</FormCode>
</QuoteHeader>
</HomeOwnersContext>

 

I wanted to remove "<burglarAlarm>No</burglarAlarm>" from the above xml.

  • Here's a quick groovy script that'll remove any and all "businessonPremises" nodes:

     

    import groovy.xml.StreamingMarkupBuilder
    import groovy.xml.XmlUtil
    
    def xmlString = "YOUR XML TEXT";
    
    def xml = new XmlSlurper(false, false).parseText(xmlString)
    
    xml.businessOnPremises.replaceNode {}
    
    log.info( groovy.xml.XmlUtil.serialize( xml ) )