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 3944842
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:56:55+00:00 2026-05-20T00:56:55+00:00

I would like to edit an existing XML file while preserving it’s original layout.

  • 0

I would like to edit an existing XML file while preserving it’s original layout. This includes new lines, comments etc. Editing is composed of finding elements inside the XML and modifying the text value.

My first try was using XMLParser with XmlUtil.serialize but that does not meet the requirement.

Anyone is aware of any alternative to XMLParser where edits are “in place” of the original XML string? If not, perhaps there is a library that performs search using XPath/GPath and just return the location of the find so I can do StringBuilder.replace.

EDIT:

For now I made this function, to find string indexes of XML node (that I can find using xpath) then I am doing replace on the indexes. Works fine for simple nodes
<node>value</node>:

def find_location_by_node(xmlString, root_xml, node)
{
    current_index = 0;

    for(current_node in root_xml.depthFirst())
    {
      node_name = current_node.name().getLocalPart()
      current_index = xmlString.indexOf('<' + node_name, current_index);

      if(current_node == node)
      {
        end_tag = '</' + node_name + '>';
        end_tag_index = xmlString.indexOf(end_tag, current_index) + end_tag.length();

        return [current_index, end_tag_index];
      }
    }

  return -1;
}
  • 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-20T00:56:56+00:00Added an answer on May 20, 2026 at 12:56 am

    You could update your XML with DOMCategory. DOM will keep your original layout.

    import groovy.xml.DOMBuilder
    
    def input = '''
    <shopping>
        <category type="groceries">
            <item>Chocolate</item>
            <item>Coffee</item>
        </category>
        <category type="supplies">
            <item>Paper</item>
            <item quantity="4">Pens</item>
        </category>
        <category type="present">
            <item when="Aug 10">Kathryn's Birthday</item>
            <item>Chocolate</item>
        </category>
    </shopping>
    
    '''
    
    def doc = DOMBuilder.parse(new StringReader(input))
    def root = doc.documentElement
    use(groovy.xml.dom.DOMCategory) {
        def chocolate = root.depthFirst().grep{it.text() == "Chocolate"}
        chocolate*.value = "Nutella"
    }
    
    def result = groovy.xml.dom.DOMUtil.serialize(root)
    println result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write a script which will edit multiple XML files, I
I have this line: $(#clients-edit-wrapper).height($(window).height()-150); I would like to apply that height function to
I have an existing XML schema that contains a type that I would like
I'm new to Zen cart and am editing an existing shop. I would like
I would like to edit matlab scripts in two cases (1) In matlab Command
I'm working in Visual Studio 2008 and I would like for Edit > Outlining
I would like to place an edit control (WC_EDIT) on a static control (WC_STATIC).
I would like to know how I could edit mp3 files on the server
I would like the user to be able to edit the title of the
EDIT : Scroll down to see the updated code. I would like to build

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.