Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 974613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:27:43+00:00 2026-05-16T03:27:43+00:00

I am parsing some XML with XMLSlurper (groovy 1.7.4) and I need to delete

  • 0

I am parsing some XML with XMLSlurper (groovy 1.7.4) and I need to delete a tag (not make it empty!). Here is a code sample illustrating it:

import groovy.xml.StreamingMarkupBuilder

def CAR_RECORDS = '''
    <records>
      <car name='HSV Maloo' make='Holden' year='2006'>
        <country>Australia</country>
        <record type='speed'>Production Pickup Truck with speed of 271kph</record>
      </car>
      <car name='P50' make='Peel' year='1962'>
        <country>Isle of Man</country>
        <record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
      </car>
      <car name='Royale' make='Bugatti' year='1931'>
        <country>France</country>
        <record type='price'>Most Valuable Car at $15 million</record>
      </car>
    </records>
  '''

def records = new XmlSlurper().parseText(CAR_RECORDS)
def allRecords = records.car
assert 3 == allRecords.size()

def firstRecord = records.car[0]
assert 'car' == firstRecord.name()
println 'country before: ' + firstRecord.'country'.text()
firstRecord.'country'.replaceNode {}

println 'country after: ' + firstRecord.'country'.text()

This prints

country before: Australia
country after: Australia

In XMLSlurper, there is no firstRecord.remove(‘country’)

I’m really puzzled. This is such an obvious thing to do…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:27:43+00:00Added an answer on May 16, 2026 at 3:27 am

    If you print out the XML from the Slurper after your call to replaceNode though:

    import groovy.xml.XmlUtil
    
    // ... your code here, followed by: ...
    
    println XmlUtil.serialize(new StreamingMarkupBuilder().bind {
      mkp.yield records
    } )
    

    The country node seems to be gone:

    <?xml version="1.0" encoding="UTF-8"?>
    <records>
      <car name="HSV Maloo" year="2006" make="Holden">
        <record type="speed">Production Pickup Truck with speed of 271kph</record>
      </car>
      <car name="P50" year="1962" make="Peel">
        <country>Isle of Man</country>
        <record type="size">Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
      </car>
      <car name="Royale" year="1931" make="Bugatti">
        <country>France</country>
        <record type="price">Most Valuable Car at $15 million</record>
      </car>
    </records>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn some XML Parsing here and I've been given some code
I need to do some XML-related job (parsing, comparison etc). Is there any C++
I am not experienced in xml parsing so maybe some of the things I
I am having trouble parsing some returned XML using this command: XML::Parser.string(xml_string).parse Here is
So i'm trying to learn some xml parsing here, and I'm getting the hang
I am using xml.dom.minidom for parsing some XML from a string. I need to
I'm trying to write a unit test that checks some xml parsing code. The
I need to parse some XML that triggers an NSURLConnection. After the parsing finished,
I'm parsing some XML with a ContentHandler, and I can get attributes within tags
I am parsing some XML that will have a link such as the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.