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

  • SEARCH
  • Home
  • 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 951781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:47:26+00:00 2026-05-15T23:47:26+00:00

With more than a little help from daviderossi.blogspot.com I have managed to get some

  • 0

With more than a little help from daviderossi.blogspot.com I have managed to get some code working to replace an xml value with another

This give me the following output which both edits the value at the ‘ix’ position BUT also adds a second copy at the end. If I search for it with LastIndexOf and delete it then it deletes the first occurrence. Any ideas on why the code might be doing that, or how to mitigate this unwanted effect??

def fm_xml = '''<?xml version="1.0" encoding="UTF-8"?>
<MAlong>
<Enquiry.ID>SC11147</Enquiry.ID>
<student.name_middle></student.name_middle>
<student.name_known></student.name_known>
<student.name_previous></student.name_previous>
<student.name_cert>John REnfrew</student.name_cert>
<student.detail_gender>M</student.detail_gender>
<student.sign_name>John Renfrew</student.sign_name>
<student.sign_date>05/01/2010</student.sign_date>
</MAlong>'''

xml = new XmlParser().parseText(fm_xml)
ix = xml.children().findIndexOf{it.name() =='student.name_middle'}
nn = new Node(xml, 'student.name_middle', "NEW")
if (ix != -1 ) {
xml.children()[ix] = nn
nn.parent = xml
}
writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(xml)
result = writer.toString()

RESULT

<MAlong>
<Enquiry.ID>
 SC11147
</Enquiry.ID>
<student.name_middle>
 NEW
</student.name_middle>
<student.name_known/>
<student.name_previous/>
<student.name_cert>
 John REnfrew
</student.name_cert>
<student.detail_gender>
 M
</student.detail_gender>
<student.sign_name>
 John Renfrew
</student.sign_name>
<student.sign_date>
 05/01/2010
</student.sign_date>
<student.name_middle>
 NEW
</student.name_middle>
</MAlong>
  • 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-15T23:47:27+00:00Added an answer on May 15, 2026 at 11:47 pm

    Using the Groovy class XMLSlurper to work with your XML makes your code easier and improves readability. I created a sample script at the Groovy Console where you can evaluate this:

    • http://groovyconsole.appspot.com/script/177001 (first version)
    • http://groovyconsole.appspot.com/script/180001 (with pretty print)

    Example-Code

    import groovy.xml.StreamingMarkupBuilder
    import groovy.xml.XmlUtil
    
    def prettyprint(xml) {
      XmlUtil.serialize(new StreamingMarkupBuilder().bind { mkp.yield xml })
    }
    
    def input = '''<?xml version="1.0" encoding="UTF-8"?><MAlong>
    <Enquiry.ID>SC11147</Enquiry.ID>
    <student.name_middle></student.name_middle>
    <student.name_known></student.name_known>
    <student.name_previous></student.name_previous>
    <student.name_cert>John REnfrew</student.name_cert>
    <student.detail_gender>M</student.detail_gender>
    <student.sign_name>John Renfrew</student.sign_name>
    <student.sign_date>05/01/2010</student.sign_date>
    </MAlong>'''
    
    def root = new XmlSlurper().parseText(input)
    println "Input\n" + prettyprint(root)
    
    // static way
    root.'student.name_middle' = "MIDDLE NAME"
    
    // variable way
    root.setProperty("student.name_previous", "PREVIOUS NAME")
    
    println "Output\n" + prettyprint(root​)​
    

    ​
    Reference:

    • http://groovy.codehaus.org/api/groovy/util/XmlSlurper.html
    • http://groovy.codehaus.org/gapi/groovy/xml/StreamingMarkupBuilder.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question seems easy but is little more theoretical than it looks. There are
I have more than one OpenID as I have tried out numerous. As people
I often have nothing more than an FTP access to a server on which
I am getting more than a little sick of this iPhone SDK and its
I am using the gmap3 jquery plugin and need a little help. I have
More than once I've lost work by accidentally killing a temporary buffer in Emacs.
More than about LINQ to [insert your favorite provider here], this question is about
On more than one occasion I've been asked to implement rules for password selection
I discovered template metaprogramming more than 5 years ago and got a huge kick
There may be more than one way to ask this question, so here's a

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.