2013-06-19 50 views
6

我使用基於xml的請求爲我的網站做了api。如何使用php驗證xml

但有時候,有些客戶發給我一個無效的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> 
+0

你也可以嘗試[與XML處理錯誤](http://php.net/manual/en/simplexml.examples-errors.php) –

+0

您需要提供我們:一個正確的XML的例子(我假設你發佈的是有效的),你的DTD,一個(或多個)WRONG XML和相關的「最佳響應」。否則,你將得不到多少幫助。 –

+0

@用戶密碼:吶,它甚至不接近我的問題... – Pooya

回答

1

您可以使用XMLReader::isValid()

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

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

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

OR:XMLReader :: readString from字符串 – user956584

+1

注意:這將檢查當前節點,而不是整個文檔。 – Amaynut

2

PHP文檔正是你所需要的!

XML DOMDocument::validate

我敢肯定,你已經定義了適當的DTD,對不對?

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

或從字符串中加載:$ dom-> loadHTML($ string); – user956584

0

您可以使用PHP函數: -

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

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

var_dump($xmlcontents->isValid()); 

來源: - http://php.net/manual/en/xmlreader.isvalid.php

+0

注意:這將檢查當前節點,而不是整個文檔。 – Amaynut

10

不幸的XMLReader沒有驗證很多事情在我的情況。

這裏一小塊類的,我寫了前段時間:

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

它正常工作與流和vfsStream以及用於測試目的。

+0

添加檢查以避免'DOMDocument :: loadXML():作爲輸入提供的空字符串錯誤 –

1

如果您只想檢查文檔是否格式正確(您不關心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"); 
}