check for valid simpleXML object before foreach()

Looks worse than it is due to indenting two sections