0

I am generating XML using StreamingMarkupBuilder. I am trying to account for scenarios were there are errors in my data, resulting in NullPointerException. This is how my data is built up currently:

def xml = new StreamingMarkupBuilder()
...
def records = Package.findAll()
def resp = { mkp ->
  ...
  records.each{ rec ->
      buildMetadata(rec, mkp, result, metadataPrefix, prefixHandler)
  }
}
xml.bind(resp)

When I get NPE in buildMetadata, the process stops mid-way, generating invalid XML.

What I need is to not include the faulty records, so I am trying something like this:

records.each{ rec ->
    def single_rec_builder = { builder ->
          buildMetadata(rec, builder, result,metadataPrefix,prefixHandler)
    }
     try{
        mkp.bind(single_rec_builder)
     }catch(Exception e){
        log.debug("Caught exception ${e.class} and continue")
     }

So in my approach I try to break down the parts added to the main structure, so I can leave out the faulty records.

Evaluation happens on .bind() call, which is already too late. If I try to force evaluation earlier, by adding something like def output = new StreamingMarkupBuilder().bind(single_rec_builder) before mkp.bind() I get namespace errors, because those are defined earlier on mkp.. Is there some sort of 'dry-run' or another better approach for this issue ?

Giannis
  • 5,286
  • 15
  • 58
  • 113
  • Why just don't you filter out the invalid data and log them for instance? – Opal Mar 09 '15 at 11:41
  • I have added null checks for the thing that was breaking, although I want the above approach to recover from scenarios that have not yet appeared. – Giannis Mar 09 '15 at 11:52

0 Answers0