How to check xml with php

I made an api with an xml request for my site.

but sometimes some clients send me invalid xml and I want to return a good answer.

how can i check the xml?

edited by:

Well, I think I asked the wrong question, I want to check the nodes, and if some nodes are missing, I will return the best answer.

I used to check this with php and I have to check all the nodes. but this method is very difficult to change.

this 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> 
+6
source share
5 answers

Unfortunately, XMLReader did not confirm many things in my case.

Here is a small piece of the class that I wrote some time ago:

 /** * Class XmlValidator * @author Francesco Casula < fra.casula@gmail.com > */ 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 great with streams and vfsStream , as well as for testing purposes.

+10
source

The PHP documentation has exactly what you need!

XML DOMDocument :: validate

I'm sure you have already determined the correct DTD, right?

 <?php $dom = new DOMDocument; $dom->Load('book.xml'); if ($dom->validate()) { echo "This document is valid!\n"; } ?> 
+2
source

You can use XMLReader::isValid() .

 <?php $xml = XMLReader::open('xmlfile.xml'); // You must to use it $xml->setParserProperty(XMLReader::VALIDATE, true); var_dump($xml->isValid()); ?> 
+1
source

If you want to check only if the document is correctly formed (you don’t care about the validity of the DTD and don’t even have a DTD for checking), use Domdocument to check your file, not XMLReader, because XMLReader will transmit the document to you and not load it right away, therefore, the isValid () method only checks the first node. You can try this code:

  $doc = new \DOMDocument(); if(@$doc->load($filePath)){ var_dump("$filePath is a valid XML document"); } else { var_dump("$filePath is NOT a valid document"); } 
+1
source

You can use the PHP function: -

 $xmlcontents = XMLReader::open('filename.xml'); $xmlcontents->setParserProperty(XMLReader::VALIDATE, true); var_dump($xmlcontents->isValid()); 

Source: - http://php.net/manual/en/xmlreader.isvalid.php

0
source

Source: https://habr.com/ru/post/947654/


All Articles