I have made an api with xml based request for my website.
but sometimes, some customers send me an invalid xml and I want to return a good response.
how can I validate xml?
edited:
Ok, I think I asked wrong question, I want to validate nodes and if some nodes missing then I return best response.
I used to validate this with php and I have to check every nodes. but this way is very hard to modify.
it is my xml example:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<mashhadhost>
<create>
<name>example.ir</name>
<period>60</period>
<ns>
<hostAttr>
<hostName>ns1.example.ir</hostName>
<hostAddr ip="v4">192.0.2.2</hostAddr>
</hostAttr>
</ns>
<contact type="holder">ex61-irnic</contact>
<contact type="admin">ex61-irnic</contact>
<contact type="tech">ex61-irnic</contact>
<contact type="bill">ex61-irnic</contact>
</create>
<auth>
<code>TOKEN</code>
</auth>
</mashhadhost>
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.
XML documents are validated by the Create method of the XmlReader class. To validate an XML document, construct an XmlReaderSettings object that contains an XML schema definition language (XSD) schema with which to validate the XML document.
The XMLReader::isValid() function is an inbuilt function in PHP which is used to check if the document being parsed is valid or not. An XML is valid if it is written by following a DTD (Document Type Definition) which defines all the allowed elements and the structure of elements.
Unfortunately XMLReader didn't validate lots of things in my case.
Here a small piece of class I wrote a while ago:
/**
* Class XmlValidator
* @author Francesco Casula <[email protected]>
*/
class XmlValidator
{
/**
* @param string $xmlFilename Path to the XML file
* @param string $version 1.0
* @param string $encoding utf-8
* @return bool
*/
public function isXMLFileValid($xmlFilename, $version = '1.0', $encoding = 'utf-8')
{
$xmlContent = file_get_contents($xmlFilename);
return $this->isXMLContentValid($xmlContent, $version, $encoding);
}
/**
* @param string $xmlContent A well-formed XML string
* @param string $version 1.0
* @param string $encoding utf-8
* @return bool
*/
public function isXMLContentValid($xmlContent, $version = '1.0', $encoding = 'utf-8')
{
if (trim($xmlContent) == '') {
return false;
}
libxml_use_internal_errors(true);
$doc = new DOMDocument($version, $encoding);
$doc->loadXML($xmlContent);
$errors = libxml_get_errors();
libxml_clear_errors();
return empty($errors);
}
}
It works fine with streams and vfsStream as well for testing purposes.
The PHP documentation has exactly what you need!
XML DOMDocument::validate
I'm sure that you have already defined the proper DTD, right?
<?php
$dom = new DOMDocument;
$dom->Load('book.xml');
if ($dom->validate()) {
echo "This document is valid!\n";
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With