views:

910

answers:

1

When I use this code to output some XML I parsed (and modified) with XmlParser

    XmlParser parser = new XmlParser()
    def root = parser.parseText(feedUrl.toURL().text)
    def writer = new StringWriter()
    new XmlNodePrinter(new PrintWriter(writer)).print(root)
    println writer.toString()

the namespace declarations on the root node are not printed, even though they are there in the toString() of root... any ideas?

+1  A: 

It looks like it's denormalizing the output and including the namespace context along with the nodes that actually need the namespace context.

For example, the webpage for this question comes in with creativeCommons namespace embedded:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:thr="http://purl.org/syndication/thread/1.0"&gt;
  <!-- snip -->
  <creativeCommons:license>http://www.creativecommons.org/licenses/by-nc/2.5/rdf&lt;/creativeCommons:license&gt;
  <!-- snip -->
</feed>

When you output the xml using this script:

def root = new XmlParser().parseText("http://stackoverflow.com/feeds/question/227447".toURL().text)
println new XmlNodePrinter().print(root)

It ends up moving the namespace to the license node that needs that namespace. Not a huge deal in this case as there is only a single node in that namespace. If most of the XML were namespaced, it'd probably bloat things quite a bit more.

<feed xmlns="http://www.w3.org/2005/Atom"&gt;
  <!-- snip -->
    <creativeCommons:license xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"&gt;
http://www.creativecommons.org/licenses/by-nc/2.5/rdf
  </creativeCommons:license>
  <!-- snip -->
</feed>

If you actually wanted the nodes normalized, you'd have to make some tweaks to the XmlNodePrinter to do 2 passes through the XML, first to gather all of the used namespaces and 2nd to output them at the top rather than within each namespaced node. The groovy source code is actually pretty readable and wouldn't be that hard to modify if you actually needed this.

Ted Naleid