how to check xml validation recursively in folders using php


<?php
function isValidXML ( $xml ) {
	libxml_use_internal_errors( true );
	$doc = new DOMDocument('1.0', 'utf-8');
	$doc->load( $xml );
	$errors = libxml_get_errors();
	libxml_clear_errors();
    return empty( $errors );
}
function debug($content){
	$filename = 'bug.txt';
	if (!file_exists($filename)){
		touch($filename);
	}
	if (is_writable($filename)) {

		if (!$handle = fopen($filename, 'a')) {
			 echo "Cannot open file ($filename)";
			 exit;
		}

		if (fwrite($handle, $content) === FALSE) {
			echo "Cannot write to file ($filename)";
			exit;
		}
		fclose($handle);

	} else {
		echo "The file $filename is not writable \n";
		echo $content;
	}
}

function checkFolder($path)
{
	if ($handle=opendir($path))
	{
		while (false!==($file=readdir($handle)))
		{
			if ($file<>"." AND $file<>"..")
			{
				if (is_file($path.'/'.$file))
				{
					$extension = substr($file, strrpos($file, ".") + 1);
					if(strtolower($extension) == "xml"){
						if(!isValidXML($path.'/'.$file)){
							debug($path.'/'.$file."\n");
						}
					}
				}
				if (is_dir($path.'/'.$file))
				{
					checkFolder($path.'/'.$file);
				}
			}
		}
	}
}
// here write the path where you have to test xml
checkFolder(__DIR__);

?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s