How do i remove a node from an xml?
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Solved! Go to Solution.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Your best bet is probably writing a custom groovy script and using that somehow. We may need more details of what your use case is, exactly, so we can better help.
---
Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am trying to build an xml dynamically- i have the code to add a node before and after a particular node.
I don't seem to know how to remove a node by passing the node name.
Ex: In the below xml, i want to remove "businessOnPremises" node
<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>
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There's a few ways to get this done, but I may need more information. What conditions do you need to meet to remove "<businessOnPremises>false</businessOnPremises>"?
What rules are you following to remove these nodes? The answer to that will play a key part in how to handle this.
---
Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There is no rule that i should follow, as the xml is simple.
I need to pass the node to be removed and the resulting xml shouldn't have that node.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 ) )
---
Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks msiadak
I improvised on it by parameterising it.
def nodeRemove = "burglarAlarm" def rootNode = new XmlSlurper(false, false).parseText(xmlString) rootNode.QuoteData.children().findAll { it.name().equals(nodeRemove) }.replaceNode {} println XmlUtil.serialize(rootNode)
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Regards,
Rao.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sure i will nmrao.
