【问题标题】:PHP DOM LoadXML processing infinite loop when xml has parse errors当xml有解析错误时PHP DOM LoadXML处理无限循环
【发布时间】:2014-03-26 19:38:26
【问题描述】:

xml数据解析错误时如何停止循环?

//xml data <column><col>Col1</col><col>Col2</col><col>Col3</col><column>

$xml = new DOMDocument();
$xml->loadXML($item_xml);

//loads the data
// find column
if(!empty(   $xml->documentElement)){
   foreach(  $xml->documentElement->childNodes as $xmlChild){
          if($xmlChild->nodeName == 'column'){
             return $xmlChild;
         }
    }
}

如果有任何 XMl 解析错误。它连续处理而不抛出任何异常。

xml解析错误如何避免?

【问题讨论】:

    标签: php dom xml-parsing


    【解决方案1】:

    你可以使用libxml_get_errors():

    $xml = new DOMDocument();
    
    // Don't display errors and warnings
    $errorState = libxml_use_internal_errors(TRUE);
    
    // Load the XML now
    $xml->loadXML($item_xml);
    
    // Get all the errors, as an array
    $errors = libxml_get_errors();
    
    // If the array is not empty
    if(!empty($errors)) {
        // Markup contains error(s)
    }
    

    Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 2016-02-07
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多