Re #9118. Protect against malformed XML.

I added a bunch of tests for the XML string not being as expected and
then refactored the code to protect against that.
Merge branch 'feature/9118_simplify_imddimensionfactory' into develop