public static function validateXML($data, &$errors, $isFile = true, $xsltProcessor = null, $encoding = 'UTF-8')
    {
        $_data = ($isFile) ? file_get_contents($data) : $data;
        $_data = preg_replace('/<!DOCTYPE[-.:"\'\/\\w\\s]+>/', null, $_data);

        if (strpos($_data, '<?xml') === false) {
            $_data = '<?xml version="1.0" encoding="'.$encoding.'"?><rootelement>'.$_data.'</rootelement>';
        }

        if (is_object($xsltProcessor)) {
            $xsl = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

            <xsl:template match="/"></xsl:template>

            </xsl:stylesheet>';

            $xsltProcessor->process($_data, $xsl, array());

            if ($xsltProcessor->isErrors()) {
                $errors = $xsltProcessor->getError(true);
                return false;
            }
        } else {
            $_parser = xml_parser_create();
            xml_parser_set_option($_parser, XML_OPTION_SKIP_WHITE, 0);
            xml_parser_set_option($_parser, XML_OPTION_CASE_FOLDING, 0);

            if (!xml_parse($_parser, $_data)) {
                $errors = array('error' => xml_get_error_code($_parser) . ': ' . xml_error_string(xml_get_error_code($_parser)),
                                'col' => xml_get_current_column_number($_parser),
                                'line' => (xml_get_current_line_number($_parser) - 2));
                return false;
            }

            xml_parser_free($_parser);
        }

        return true;
    }
public static function validateXML($data, &$errors, $isFile = true, $xsltProcessor = null, $encoding = 'UTF-8')
    {
        $_data = ($isFile) ? file_get_contents($data) : $data;
        $_data = preg_replace('/<!DOCTYPE[-.:"\'\/\\w\\s]+>/', null, $_data);

        if (strpos($_data, '<?xml') === false) {
            $_data = '<?xml version="1.0" encoding="'.$encoding.'"?><rootelement>'.$_data.'</rootelement>';
        }

        if (is_object($xsltProcessor)) {
            $xsl = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

            <xsl:template match="/"></xsl:template>

            </xsl:stylesheet>';

            $xsltProcessor->process($_data, $xsl, array());

            if ($xsltProcessor->isErrors()) {
                $errors = $xsltProcessor->getError(true);
                return false;
            }
        } else {
            $_parser = xml_parser_create();
            xml_parser_set_option($_parser, XML_OPTION_SKIP_WHITE, 0);
            xml_parser_set_option($_parser, XML_OPTION_CASE_FOLDING, 0);

            if (!xml_parse($_parser, $_data)) {
                $errors = array('error' => xml_get_error_code($_parser) . ': ' . xml_error_string(xml_get_error_code($_parser)),
                                'col' => xml_get_current_column_number($_parser),
                                'line' => (xml_get_current_line_number($_parser) - 2));
                return false;
            }

            xml_parser_free($_parser);
        }

        return true;
    }
public static function validateXML ( $ data , & $ errors , $ isFile = true , $ xsltProcessor = null , $ encoding = 'UTF-8' ) { $ _data = ( $ isFile ) ? file_get_contents ( $ data ) : $ data ; $ _data = preg_replace ( '/<!DOCTYPE[-.:"\'\/\\w\\s]+>/' , null , $ _data ) ; if ( strpos ( $ _data , '<?xml' ) === false ) { $ _data = '<?xml version="1.0" encoding="' . $ encoding . '"?><rootelement>' . $ _data . '</rootelement>' ; } if ( is_object ( $ xsltProcessor ) ) { $ xsl = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

            <xsl:template match="/"></xsl:template>

            </xsl:stylesheet>' ; $ xsltProcessor -> process ( $ _data , $ xsl , array ( ) ) ; if ( $ xsltProcessor -> isErrors ( ) ) { $ errors = $ xsltProcessor -> getError ( true ) ; return false ; } } else { $ _parser = xml_parser_create ( ) ; xml_parser_set_option ( $ _parser , XML_OPTION_SKIP_WHITE , 0 ) ; xml_parser_set_option ( $ _parser , XML_OPTION_CASE_FOLDING , 0 ) ; if ( ! xml_parse ( $ _parser , $ _data ) ) { $ errors = array ( 'error' => xml_get_error_code ( $ _parser ) . ': ' . xml_error_string ( xml_get_error_code ( $ _parser ) ) , 'col' => xml_get_current_column_number ( $ _parser ) , 'line' => ( xml_get_current_line_number ( $ _parser ) - 2 ) ) ; return false ; } xml_parser_free ( $ _parser ) ; } return true ; }
Checks an xml document for well-formedness.

@param string $data
filename, xml document as a string, or arbitrary string
@param pointer &$errors
pointer to an array which will contain any validation errors
@param boolean $isFile (optional)
if this is true, the method will attempt to read from a file, `$data`
instead.
@param XsltProcess $xsltProcessor (optional)
if set, the validation will be done using this XSLT processor rather
than the built in XML parser. the default is null.
@param string $encoding (optional)
if no XML header is expected, than this should be set to match the
encoding of the XML
@return boolean
true if there are no errors in validating the XML, false otherwise.
Checks an xml document for well - formedness .