How can XML files be validated using an XSD schema in PHP?
To validate XML files using an XSD schema in PHP, you can use the `DOMDocument` class along with the `schemaValidate` method. First, load the XML file into a `DOMDocument` object, then set the XSD schema using the `schemaValidateSource` method. Finally, call the `schemaValidate` method to validate the XML file against the XSD schema.
$xml = new DOMDocument();
$xml->load('file.xml');
if ($xml->schemaValidateSource(file_get_contents('schema.xsd'))) {
echo 'XML is valid.';
} else {
echo 'XML is invalid.';
}
Keywords
Related Questions
- What is the significance of passing a complete path to the filetype() function in PHP?
- How can PHP developers ensure that user input is properly sanitized and validated before using it in output statements like "Herzlich Willkommen"?
- How can PHP code for form validation and data retrieval be optimized for efficiency and security?