0

In my xml file, that I read from a URL using

def inp = url.openStream(...)
def slurper = new XmlSlurper()
def xml = slurper.parse(inp).declareNamespace(xml:'http://www.w3.org/XML/1998/namespace')

I have

<course xmlns="http://www.kth.se/student/kurser" code="DD2471">
  <title xmlns="" xml:lang="sv">Moderna databassystem</title>
  <title xmlns="" xml:lang="en">Modern Database Systems</title>

and I extract the titles with groovy 1.8.6 by

def name = xml.title.find{ it.@':lang' == 'sv' }.text()

But when upgrading to groovy 2.2.2 I no longer get the swedish title just an empty string and I can't figure out how to extract the title. I want to find a method that works in all groovy versions (or >= 1.8.6)

serafim
  • 75
  • 1
  • 1
  • 9
  • could you try ``xml.title.find{ it.'@xml:lang'=='sv' }.text()`` with your older groovy version? ``:lang`` means (in newer version) *no namespace* – cfrick Aug 28 '14 at 06:35
  • '@xml:lang' works both with and without name space declaration in groovy 1.8.6 – serafim Aug 28 '14 at 07:34
  • @cfrick: but when I change to groovy 2.2.2 '@xml:lang' does not work – serafim Aug 28 '14 at 08:00
  • yes, it does not -- i have tested it now with 2.2.2 and can confirm. i have added the versions to my answer for now to reflect this – cfrick Aug 28 '14 at 08:08

2 Answers2

3

Works in 1.8.6 and 2.3.4 -- not in 2.2.2, as there is a bug when accessing the xml NS.

xml.title.find{ it.'@xml:lang'=='sv' }.text()

':lang' (without NS prefix) means (in newer groovy versions) no namespace.

see Reading XML using Groovy's XmlSlurper

  • name or "*:name" matches an element named "name" irrespective of the namespace it's in (i.e. this is the default mode of operation)
  • ":name" matches an element named "name" only id the element is not in a namespace
  • "prefix:name" matches an element names "name" only if it is in the namespace identified by the prefix "prefix" (and the prefix to namespace mapping was defined by a previous call to declareNamespace)
cfrick
  • 35,203
  • 6
  • 56
  • 68
  • I will try upgrading but that demands upgrading Java as well and grails and ... and that will in turn be a lot of work on the production server. I'll be back ... but it will take some time. Many thanks, though. Maybe test XmlParser, but that also means a lot of work – serafim Aug 28 '14 at 10:40
  • @serafim i have tried some more versions of it and all does fail with 2.2.2. maybe you should file a bug with the groovy folks. i would expect, that this is a regression. – cfrick Aug 28 '14 at 10:58
  • @serafim yeah im pretty sure it is. using another namespace than `xml` makes your example work with *2.2.2*. The comment in the ticket mentions, that they are willing to backport. – cfrick Aug 28 '14 at 11:08
  • I have set up a working groovy 2.3.6 with grails 2.4.3 and Java 7u65 (ubuntu) on my home workstation and found that all my earlier strategies @lang, @':lang', @'xml:lang', '@:lang', '@xml:lang' work in that setting. I guess I settle for @'xml:lang' as it conforms with other patterns (like @'first-name' ...). Again many thanks for straightening things out. – serafim Aug 28 '14 at 14:28
  • Also tested with oracle java8 (latest) and all works well. Remains to upgrade the production server. Please notify me if @'xml:lang' is a bad choise of pattern. – serafim Aug 28 '14 at 15:57
  • @serafim it does not matter actually. in the end it all will be handled by a catch-all method within the class. `.@` accesses the pure private var in a regular class (no implict getter/setter call) so i prefer '@xxx' for pure styling reasons. good luck with your server. – cfrick Aug 28 '14 at 16:02
0

The problem being that I ran into a bug in the groovy version (2.2.2.) that I use in my application the only reasonable conclusion is to upgrade to a version in which the bug is corrected. Thus, the solution is upgrading to the latest groovy (2.3.6) and grails (2.4.3)

An alternative solution would be to use XmlParser instead of XmlSlurper, which is what I presently use.

I also had to upgrade from JDBC3 to JDBC4.

And, finally, when the company moves from Ubuntu 12.04 to Ubuntu 14.04, I will upgrade to Postgresql 9.3.5

All these upgrades were already planned to eventually happen but the groovy bug forced the pace and we took longer steps, skipping som intermediate versions.

serafim
  • 75
  • 1
  • 1
  • 9