PHP | DOMDocument validate() Function Last Updated : 26 Feb, 2020 Comments Improve Suggest changes Like Article Like Report The DOMDocument::validate() function is an inbuilt function in PHP which is used to validate the document based on its DTD (Document Type Definition). DTD defines the rules or structure to be followed by the XML file and if a XML document doesn't follows this format then this function will return false. Syntax: bool DOMDocument::validate( void ) Parameters: This function doesn’t accept any parameters. Return Value: This function returns TRUE if document follows the DTD or FALSE. Below examples illustrate the DOMDocument::validate() function in PHP: Example 1: php <?php // Create a new DOMDocument $doc = new DOMDocument; // Load the XML with DTD rule to have // a root element with first, second, // and third as its three children $doc->loadXML("<?xml version=\"1.0\"?> <!DOCTYPE root [ <!ELEMENT root (first, second, third)> <!ELEMENT first (#PCDATA)> <!ELEMENT second (#PCDATA)> <!ELEMENT third (#PCDATA)> ]> <!-- Create a XML following the DTD --> <root> <first>Hello</first> <second>There</second> <third>World</third> </root>"); // Check if XML follows the DTD rule if ($doc->validate()) { echo "This document is valid!\n"; } ?> Output: This document is valid! Example 2: php <?php // Create a new DOMDocument $doc = new DOMDocument; // Load the XML $doc->loadXML("<?xml version=\"1.0\"?> <!DOCTYPE root [ <!ELEMENT root (one, two, three)> <!ELEMENT one (#PCDATA)> <!ELEMENT two (#PCDATA)> <!ELEMENT three (#PCDATA)> ]> <!-- Create a XML voilating the DTD --> <root> <one>Hello</one> <two>World</two> </root>"); if (!$doc->validate()) { echo "This document is not valid!"; } ?> Output: This document is not valid! Reference: https://www.php.net/manual/en/domdocument.validate.php Comment More infoAdvertise with us Next Article PHP | DOMDocument validate() Function gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMDocument schemaValidate() Function The DOMDocument::schemaValidate() function is an inbuilt function in PHP which is used to validate a document based on the given schema file. The schema file can be in an XSD format which is the recommendation from W3C (World Wide Web Consortium). Syntax: bool DOMDocument::schemaValidate( string $fi 2 min read PHP | DOMDocument relaxNGValidate() Function The DOMDocument::relaxNGValidate() function is an inbuilt function in PHP which is used to performs relaxNG validation on the document. The relaxNG is an alternative to DDT and defines a structure which needs to be followed by the XML document. Syntax: bool DOMDocument::relaxNGValidate( string $file 2 min read PHP | DOMDocument save() Function The DOMDocument::save() function is an inbuilt function in PHP which is used to create an XML document from the DOM representation. This function is used after creating the new dom document from scratch. Syntax: int DOMDocument::save( string $filename, int $options = 0 ) Parameters: This function ac 2 min read PHP | DOMDocument schemaValidateSource() Function The DOMDocument::schemaValidateSource() function is an inbuilt function in PHP which is used to validate a document based on a schema defined in the given string. The difference between schemaValidate() and schemaValidateSource() is that the former accepts a schema filename whereas latter can accept 2 min read PHP | DOMDocument xinclude() Function The DOMDocument::xinclude() function is an inbuilt function in PHP which is used to substitute the XIncludes in a DOMDocument Object. Syntax: int DOMDocument::xinclude( int $options = 0 ) Parameters: This function accepts an optional single parameter $options which holds the libxml parameter. Return 2 min read PHP | DOMDocument relaxNGValidateSource() Function The DOMDocument::relaxNGValidateSource() function is an inbuilt function in PHP which is used to perform relaxNG validation on the document using a string as RNG schema. The difference between relaxNGValidate() and relaxNGValidateSource() is that the former accepts a rng schema filename whereas latt 2 min read PHP DOMDocument saveHTMLFile() Function The DOMDocument::saveHTMLFile() function is an inbuilt function in PHP that is used to create an HTML document from the DOM representation. This function is used after creating the dom document. Syntax: int DOMDocument::saveHTMLFile( string $filename ) Parameters: This function accepts a single para 2 min read PHP | DOMDocument saveXML() Function The DOMDocument::saveXML() function is an inbuilt function in PHP which is used to create an XML document from the DOM representation. This function is used after building a new dom document from scratch. Syntax: string DOMDocument::saveXML( DOMNode $node, int $options = 0 ) Parameters: This functio 2 min read PHP DOMDocument saveHTML() Function The DOMDocument::saveHTML() function is an inbuilt function in PHP that is used to create an HTML document from the DOM representation. This function is used after building the dom document from scratch. Syntax: string DOMDocument::saveHTML( DOMNode $node = NULL ) Parameters: This function accepts s 2 min read PHP | XMLWriter endDocument() Function The XMLWriter::endDocument() function is an inbuilt function in PHP which is used to end current document. Syntax: bool XMLWriter::endDocument( void ) Parameters: This function doesnât accept any parameter. Return Value: This function returns TRUE on success or FALSE on failure. Below examples illus 1 min read Like