php 如何用php验证xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17191866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to validate xml with php
提问by Pooya
I have made an api with xml based request for my website.
我为我的网站制作了一个基于 xml 的请求的 api。
but sometimes, some customers send me an invalid xml and I want to return a good response.
但有时,一些客户给我发送了一个无效的 xml,我想返回一个好的响应。
how can I validate xml?
如何验证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.
我曾经用 php 验证过,我必须检查每个节点。但是这种方式很难修改。
it is my xml example:
这是我的 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>
回答by Francesco Casula
Unfortunately XMLReader didn't validate lots of things in my case.
不幸的是,在我的案例中,XMLReader 没有验证很多东西。
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 vfsStreamas well for testing purposes.
它适用于流和vfsStream以及用于测试目的。
回答by Amaynut
If you want to check only if the document is well formed (you don't care about the DTD validity, and don't even have a DTD to validate against) use Domdocument to validate your file, rather than XMLReader, because XMLReader will stream you document and don't load it at once, so isValid() method will only check the first node. You can try a code like this:
如果您只想检查文档是否格式正确(您不关心 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");
}
回答by STT LCU
The PHP documentation has exactly what you need!
PHP 文档正是您所需要的!
I'm sure that you have already defined the proper DTD, right?
我确定您已经定义了正确的 DTD,对吗?
<?php
$dom = new DOMDocument;
$dom->Load('book.xml');
if ($dom->validate()) {
echo "This document is valid!\n";
}
?>
回答by Praveen Kumar Purushothaman
You can use XMLReader::isValid()
.
您可以使用XMLReader::isValid()
.
<?php
$xml = XMLReader::open('xmlfile.xml');
// You must to use it
$xml->setParserProperty(XMLReader::VALIDATE, true);
var_dump($xml->isValid());
?>
回答by Vivek Sadh
You can use PHP function:-
您可以使用 PHP 函数:-
$xmlcontents = XMLReader::open('filename.xml');
$xmlcontents->setParserProperty(XMLReader::VALIDATE, true);
var_dump($xmlcontents->isValid());