/** * Create XML \DOMDocument from a string * @param string $xml - XML string document * @return DOMDocument */ public static function createXmlDocumentFromStr($xml, $docOptions = XMLUTIL_OPT_DONT_FIX_AMPERSAND) { set_error_handler(function ($number, $error) { $matches = []; if (preg_match('/^DOMDocument::loadXML\\(\\): (.+)$/', $error, $matches) === 1) { throw new \InvalidArgumentException("[Err #{$number}] " . $matches[1]); } }); $xmldoc = self::createXmlDocument($docOptions); $xmlFixed = XmlUtil::fixXmlHeader($xml); if (($docOptions & XMLUTIL_OPT_DONT_FIX_AMPERSAND) != XMLUTIL_OPT_DONT_FIX_AMPERSAND) { $xmlFixed = str_replace("&", "&", $xmlFixed); } $xmldoc->loadXML($xmlFixed); XmlUtil::extractNameSpaces($xmldoc); restore_error_handler(); return $xmldoc; }