Spade2'; $previous = libxml_use_internal_errors(true); try { $xml = simplexml_load_string($source,SimpleXMLElement::class,LIBXML_NONET); if ($xml === false) throw new RuntimeException("Invalid XML"); foreach ($xml->loan as $loan) echo (string)$loan["id"], ": ", (string)$loan->item, "\n"; } finally { libxml_clear_errors(); libxml_use_internal_errors($previous); }