Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How check if a String is a Valid XML with-out Displaying a Warning in PHP

i was trying to check the validity of a string as xml using this simplexml_load_string()Docs function but it displays a lot of warning messages.

How can I check whether a string is a valid XML without suppressing (@ at the beginning) the error and displaying a warning function that expec

like image 209
jspeshu Avatar asked Dec 29 '10 12:12

jspeshu


1 Answers

Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards.

Simple XML loading string

libxml_use_internal_errors(true);  $doc = simplexml_load_string($xmlstr); $xml = explode("\n", $xmlstr);  if (!$doc) {     $errors = libxml_get_errors();      foreach ($errors as $error) {         echo display_xml_error($error, $xml);     }      libxml_clear_errors(); } 
like image 121
Tjirp Avatar answered Sep 20 '22 21:09

Tjirp