xmlgroovygpath

Is there a better way to find ancestors in xml using Groovy Gpath?


Please see my below groovy code which is working as expected - but wondered if there is a better way to get ancestor info?

My sample xml record:

String record = '''
<collections>
  <material>
    <books>
      <title>Italy</title>
    </books>
  </material>
  <material>
    <books>
      <title>Greece</title>
    </books>  
  </material>
  <material>
    <books>
      <author>Germany</author>
    </books>  
  </material>  
  <material>
    <cd>
      <author>France</author>
    </cd>  
  </material>  
</collections>
'''

I am wondering if there is any better way to optimise this bit of getting the ancestors ?

GPathResult extractedMaterialBlocks = extractAllMaterialBlocks(record)
String finalXml = serializeXml(extractedMaterialBlocks.parent().parent(), 'UTF-8') 
println "finalXml : ${finalXml}"

My methods:

GPathResult extractAllMaterialBlocks(String record) {
    GPathResult result = new XmlSlurper().parseText(record)
    return result ? result.'material'?.'books'?.'title'?.findAll { it }  : null
}

String serializeXml(GPathResult xmlToSerialize, String encoding) {
    def builder = new StreamingMarkupBuilder()
    builder.encoding = encoding
    builder.useDoubleQuotes = true

    return builder.bind {
        out << xmlToSerialize
    }.toString()
}

Output as expected:

<material>
    <books>
      <title>Italy</title>
    </books>
</material>
<material>
    <books>
      <title>Greece</title>
    </books>  
</material>

Solution

  • You don't need to get the ancestors, if you don't dive too deep. If you want the material nodes, get those with a condition on their children instead of gettint the childs and then going up again. Your whole code can be condensed to this one line:

    System.out.println new StreamingMarkupBuilder().bind { out << new XmlSlurper().parseText(record).material.findAll { it.books.title.size() } }