validate a xml file against a xsd using php

varuog picture varuog · Jul 25, 2012 · Viewed 24.1k times · Source

how to validate a xml file against a xsd? there is domdocument::schemaValidate() but It does not tell where are the errors. is there any class for that? does it have any worth making that parser from scratch? or is it just reinventing he wheel,

Answer

Ed Heal picture Ed Heal · Jul 25, 2012

This code does the business:

$xml= new DOMDocument();
$xml->loadXML(<A string goes here containing the XML data>, LIBXML_NOBLANKS); // Or load if filename required
if (!$xml->schemaValidate(<file name for the XSD file>)) // Or schemaValidateSource if string used.
{
   // You have an error in the XML file
}

See the code in http://php.net/manual/en/domdocument.schemavalidate.php To retrieve the errors.

I.e.

justin at redwiredesign dot com 08-Nov-2006 03:32 post.