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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:57:30+00:00 2026-05-26T09:57:30+00:00

I’m using Groovy and I’m trying to insert an xml node into a xml

  • 0

I’m using Groovy and I’m trying to insert an xml node into a xml document parsed with XmlSlurper.
I manage to add the node at the end of the document but not where I really need to.

Original doc:

<xml-fragment xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config"> 
    <ser:coreEntry isProxy="true" isEnabled="true" isTracingEnabled="false">
        <ser:binding type="SOAP" isSoap12="false" xsi:type="con:SoapBindingType" xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
          <con:wsdl ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
          <con:port>
            <con:name>ChargeServicesPort</con:name>
            <con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
          </con:port>
          <con:selector type="SOAP body"/>
        </ser:binding>
    </ser:coreEntry>
</xml-fragment>

Fragment to add

def fragmentToAddXml = '''
<ser:security xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config">hello</ser:security>
'''

This is the code I’m using.

def root = new XmlSlurper().parseText(file.getText())

root.'core-entry'.appendNode( fragmentToAddXml )
def xmlBuilder = new groovy.xml.StreamingMarkupBuilder().bind{ mkp.yield root }

Please note that the new node should be placed before the “ser:binding” node.

The result should be:

<xml-fragment xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config"> 
        <ser:coreEntry isProxy="true" isEnabled="true" isTracingEnabled="false">
            <ser:security xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config">hello</ser:security>

            <ser:binding type="SOAP" isSoap12="false" xsi:type="con:SoapBindingType" xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
              <con:wsdl ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
              <con:port>
                <con:name>ChargeServicesPort</con:name>
                <con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
              </con:port>
              <con:selector type="SOAP body"/>
            </ser:binding>
        </ser:coreEntry>
    </xml-fragment>

Thanks

Luciano

  • 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-26T09:57:30+00:00Added an answer on May 26, 2026 at 9:57 am

    Given the xml (in a string for testing)

    def xml = '''<xml-fragment xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config"> 
        <ser:coreEntry isProxy="true" isEnabled="true" isTracingEnabled="false">
            <ser:binding type="SOAP" isSoap12="false" xsi:type="con:SoapBindingType" xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
              <con:wsdl ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
              <con:port>
                <con:name>ChargeServicesPort</con:name>
                <con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
              </con:port>
              <con:selector type="SOAP body"/>
            </ser:binding>
        </ser:coreEntry>
    </xml-fragment>'''
    

    And the xml you want to add as:

    def toadd = '''<ser:security xmlns:ser="http://www.bea.com/wli/sb/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:con="http://www.bea.com/wli/sb/pipeline/config">
      hello
    </ser:security>'''
    

    Then you can parse them both (with XmlSlurper set to use namespaces via the 2nd true parameter)

    def root = new XmlSlurper( false, true ).parseText( xml )
    fragmentToAdd = new XmlSlurper( false, true ).parseText( toadd )
    

    Append the xml to add to the data node (as you want it inside data, not lastname)

    root.coreEntry.appendNode( fragmentToAdd )
    

    Then print it out:

    String outxml = groovy.xml.XmlUtil.serialize( root )
    println outxml
    

    Which prints:

    <?xml version="1.0" encoding="UTF-8"?>
    <xml-fragment>
      <ser:coreEntry xmlns:ser="http://www.bea.com/wli/sb/services" isTracingEnabled="false" isProxy="true" isEnabled="true">
        <ser:binding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" isSoap12="false" xsi:type="SOAP">
          <con:wsdl xmlns:con="http://www.bea.com/wli/sb/services/bindings/config" ref="bus/src/main/osb/interfaces/apilink/ChargeServices"/>
          <con:port xmlns:con="http://www.bea.com/wli/sb/services/bindings/config">
            <con:name>ChargeServicesPort</con:name>
            <con:namespace>java:dk.tdc.apilink.logic.sessions.interfaces</con:namespace>
          </con:port>
          <con:selector xmlns:con="http://www.bea.com/wli/sb/services/bindings/config" type="SOAP body"/>
        </ser:binding>
        <ser:security>
        hello
      </ser:security>
      </ser:coreEntry>
    </xml-fragment>
    

    Which I believe is correct (not formatted 100% as I’d like, but correct) 😉

    Edit

    If order is important, you can use XmlParser like so:

    def root = new XmlParser( false, true ).parseText( xml )
    fragmentToAdd = new XmlParser( false, true ).parseText( toadd )
    
    // Insert this new node at position 0 in the children of the first coreEntry node
    root.find { it.name() == 'ser:coreEntry' }.children().add( 0, fragmentToAdd )
    
    String outxml = groovy.xml.XmlUtil.serialize( root )
    println outxml
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.