When I use this code to output some XML I parsed (and modified) with XmlParser
XmlParser parser = new XmlParser()
def root = parser.parseText(fe
I've just had the same problem and after a bit of fiddling I've found a workaround.
You use the XmlSluper instead of the XmlParser and use StreamingMarkupBuilder instead of XmlNodePrinter. Then you take advantage of the closure in bind and use the mkp built-in variable to declare the namespaces.
For example; using the source xml example of Ted's from above:
def root = new XmlSlurper().parseText("http://stackoverflow.com/feeds/question/227447".toURL().text))
def outputBuilder = new StreamingMarkupBuilder()
String result = XmlUtil.serialize(outputBuilder.bind {
mkp.declareNamespace('':'http://www.w3.org/2005/Atom')
mkp.declareNamespace('creativeCommons':'http://backend.userland.com/creativeCommonsRssModule')
mkp.declareNamespace('re':'http://purl.org/atompub/rank/1.0')
mkp.yield root }
)
println result
Results in :
How do I print a groovy Node with namespace preserved? - Stack Overflow
most recent 30 from stackoverflow.com
2011-02-16T05:13:17Z
http://stackoverflow.com/feeds/question/227447
http://www.creativecommons.org/licenses/by-nc/2.5/rdf
http://stackoverflow.com/questions/227447/how-do-i-print-a-groovy-node-with-namespace-preserved
2