Validating XML files within PHP 4 applications
Today I ran into a small problem, validating import XML data against a DTD(Document Type Definition), and discovered a neat way to solve this without PHP4's own means, as the PHP 4 version on the running system wasn't supporting the DOM XML Functions.
Xmllint, a command line XML tool, parses provided XML files and can be used to verify that these files are well formed and are satisfying an associated DTD/XML Schema.
The following method body shows an example call of the xmllint tool for validating a given XML file against a DTD. The location of the DTD file is wrapped inside a DOCTYPE definition of the import file and will be automatically resolved by xmllint.
<?phpIn addition xmllint is also very helpfull when writing custom DTDs or XML Schema, as you get an immediate reply regarding the correctness of the definition rules and their 'impact' on the associated XML files.
...
function isValidImportFile($filename) {
if($filename == '' || !$this->isSupposedFiletype($filename, 'xml')) {
return false;
}
$xmllintCall = "xmllint --valid --noout {$this->_xmlDirectory}{$filename}";
exec(escapeshellcmd($xmllintCall), $xmllintOutput, $xmllintFeedback);
if($xmllintFeedback == '0') {
return true;
} else {
// Maybe log xmllint feedback here for further evaluation.
return false;
}
}
...
3 comments:
You want to use escapeshellarg() :)
if somebody need the output error try:
$xmllint_cmd = 'xmllint --schema ' . escapeshellarg($xml_schema_path) . ' ' . escapeshellarg($xml_file_path) . ' 2>&1' ;
exec($xmllint_cmd, $xmllint_output, $xmllint_feedback );
the trick is in 2>&1
if somebody want the error output try :
$xmllint_cmd = 'xmllint --schema ' . escapeshellarg($xml_schema_path) . ' ' . escapeshellarg($xml_file_path) . ' 2>&1' ;
exec($xmllint_cmd, $xmllint_output, $xmllint_feedback );
the trick is the 2>&1 at the end of the commend
Post a Comment