繁体   English   中英

如何使用php验证xml

[英]How to validate xml with php

我对我的网站做了一个基于xml的api。

但有时,有些客户向我发送了无效的xml,而我想返回一个良好的响应。

如何验证xml?

编辑:

好的,我想我问错了一个问题,我想验证节点,如果缺少某些节点,则返回最佳响应。

我曾经用php来验证这一点,我必须检查每个节点。 但是这种方式很难修改。

这是我的xml示例:

<?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>

不幸的是,在我的案例中,XMLReader没有验证很多事情。

这是我前一段时间写的一小节课:

/**
 * 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);
    }
}

它也可以与流和vfsStream一起很好地用于测试目的。

如果您只想检查文档的格式是否正确(您不在乎DTD的有效性,甚至没有DTD进行验证),请使用Domdocument而不是XMLReader来验证文件,因为XMLReader将流您在文档中不立即加载它,因此isValid()方法将仅检查第一个节点。 您可以尝试这样的代码:

 $doc = new \DOMDocument();

 if(@$doc->load($filePath)){
     var_dump("$filePath is a valid XML document");
} else {
      var_dump("$filePath is NOT a valid document");
}

您可以使用XMLReader::isValid()

<?php
    $xml = XMLReader::open('xmlfile.xml');

    // You must to use it
    $xml->setParserProperty(XMLReader::VALIDATE, true);

    var_dump($xml->isValid());
?>

PHP文档正是您所需要的!

XML DOMDocument :: validate

我确定您已经定义了正确的DTD,对吗?

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

您可以使用PHP函数:-

$xmlcontents = XMLReader::open('filename.xml');

$xmlcontents->setParserProperty(XMLReader::VALIDATE, true);

var_dump($xmlcontents->isValid());

资料来源: -http : //php.net/manual/zh/xmlreader.isvalid.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM