groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Wagenleitner <>
Subject Re: XmlSlurper Namespace Question
Date Fri, 15 Apr 2016 18:18:01 GMT
Are you doing more than just removing nodes?  The following seems to retain
the namespace prefixes when serializing back out after removing a node.

import groovy.xml.*

String xml = '''
<a:root xmlns:a="urn:a" xmlns:b="urn:b" xmlns:c="urn:c">

def root = new XmlSlurper().parseText(xml)
root.child2.replaceNode {}

String newXml = XmlUtil.serialize(root)
def newRoot = new XmlSlurper().parseText(newXml)

println XmlUtil.serialize(newRoot)
assert newRoot.lookupNamespace('a') == 'urn:a'
assert newRoot.child3.lookupNamespace('c') == 'urn:c'

Don't believe there's a defined way to get all the namespaces from the
original files, other than possibly picking at the
non-public namespaceTagHints field or by visiting each node and using
#namespaceURI() to get it's namespace.

On Mon, Apr 11, 2016 at 9:40 AM, garneke <> wrote:

> I have a requirement in my application to receive xml files from a user and
> filter out specific nodes based on a configuration option ( accepts GPath
> String ).
> This is fine.
> I can use XmlSlurper to parse the file and with the defined GPath I can
> find
> and remove the node and rewrite the file.
> My application is generic and can accept any XML.
> My user base is not terribly savvy so I need to be able to have the GPath
> specified without the namespace prefixes.   This is also fine if the
> XmlSlurper is created to be namespace aware.
> *The problem is...*
> If my XmlSlurper is namespace aware and I remove a node, when I re-write
> the
> XML file all of the namespace prefixes get altered to "tag0:", "tag1:",
> "tag2:" etc.
> Is there a way to produce the XML with its original namespace prefixes?
> Is there someway I can query the original file for its namespaces and use
> that declare the namespaces for the slurper?
> Thanks in advance for your help.
> --
> View this message in context:
> Sent from the Groovy Users mailing list archive at

View raw message