Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate xml with php

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>
like image 401
Pooya Avatar asked Jun 19 '13 13:06

Pooya


People also ask

How to validate XML file in PHP?

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.

How do I validate an XML file?

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.

Is XML valid PHP?

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.


2 Answers

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.

like image 88
Francesco Casula Avatar answered Oct 06 '22 05:10

Francesco Casula


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";
}
?>
like image 20
STT LCU Avatar answered Oct 06 '22 05:10

STT LCU