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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:20:44+00:00 2026-06-15T15:20:44+00:00

Scenario Need to update some attributes in an existing XML-file. The file contains a

  • 0

Scenario

Need to update some attributes in an existing XML-file. The file contains a XSL processing instruction, so when the XML is parsed and updated I need to add the instruction before writing it to a file again. Problem is – whatever I do – I’m not able to insert the processing instruction

Based on the Java-example found at rgagnon.com I have created the code below

Example code ##

import groovy.xml.*

def xml = '''|<something>
            |  <Settings>
            |  </Settings>
            |</something>'''.stripMargin()

def document = DOMBuilder.parse( new StringReader( xml ) )
def pi = document.createProcessingInstruction('xml-stylesheet', 'type="text/xsl"    href="Bp8DefaultView.xsl"');
document.insertBefore(pi, document.documentElement) 

println document.documentElement

Creates output

<?xml version="1.0" encoding="UTF-8"?>
<something>
  <Settings>
  </Settings>
</something>

What I want

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Bp8DefaultView.xsl"?>
<something>
  <Settings>
  </Settings>
</something>
  • 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-06-15T15:20:46+00:00Added an answer on June 15, 2026 at 3:20 pm

    You’re inserting the PI before the documentElement, but then passing only the element to println. Does it work if you print the whole document?

    Failing that, the “official” way to output a DOM document is to use LSSerializer

    def ser = document.implementation.createLSSerializer()
    new File("output.xml").withOutputStream { o ->
      def lso = document.implementation.createLSOutput()
      lso.byteStream = o
      ser.write(document, lso)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: Textbox(Multi-line) Panel Different controls inside panel (buttons,textbox) Scenario: I need to add
I have a scenario where I need to update a list item, but I
Scenario : i have a database table that is being updated frequently by some
I need some clarification how MS-DTC will behave in scenario given below 1) I
Need some expert suggestions on using '$in' while performing 'update' in mongodb collections. Here
I need to update some fixed-priority elements in a PriorityQueue based on their ID.
I need some help from you all.. Actual Scenario is : This is Module
unihere's the scenario: I have a 1.8 million line text file that I need
Scenario : You need to expose the same app on different screens - let's
Here's my scenario: I need to make three or four calls to different WCF

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.