How to check for errors in an XML

6

I'm using the simplexml_load_string function to load a dynamic XML, but if there are formatting errors in this XML it only returns me false and I do not know where the error is, can I check this?

    
asked by anonymous 02.01.2014 / 14:21

2 answers

5

You can create a simple function to check if the XML is valid.

 <?php
function is_valid_xml ( $xml ) {
    libxml_use_internal_errors( true );

    $doc = new DOMDocument('1.0', 'utf-8');

    $doc->loadXML( $xml );

    $errors = libxml_get_errors();

    return empty( $errors );
}
?>
    
02.01.2014 / 14:44
4

According to the php.net , if you load the

02.01.2014 / 14:29