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 ?