簡體   English   中英

讀取XML並在PHP中使用XMLReader驗證架構時出錯

[英]Error while reading an XML and validating the schema with XMLReader in PHP

我有以下PHP代碼塊:

    if (file_exists($name)) {
    $reader = new XMLReader();
    $reader->open($name);
    $reader->setSchema('inc/schema.xsd');

    while($reader->read());
    $reader->close();
} else {
    die("you're having trouble with the files!");
}

對於某些網址,出現以下錯誤:

警告:XMLReader :: read():第43行的xml2csv.php中.. \\ xmlschemas.c:28351處的未實現塊

警告:XMLReader :: read():在第43行讀取xml2csv.php時發生錯誤

警告:DOMDocument :: load():products.xml中的標簽Products行1中的數據過早結束,行55:xml2csv.php中的532571

通常,導致我出現此問題的URL是本地URL(當$ name =“ file.xml”時)或遠程URL(當$ name =“ http://www.domain.com/products.xml ”時)被打斷(返回404、500等)。

對錯誤的簡單Google搜索使我想到了這一點: https : //github.com/Chronic-Dev/libxml2/blob/master/xmlschemas.c

在提到的28351行上,似乎有一條文字說TODO。 因為我的C技能非常有限(幾乎沒有),所以我想更好地理解此錯誤的原因以及如何避免碰到它。

如XMLReader所述,打開方法返回布爾值,如果成功則返回TRUE,失敗則返回FALSE。 您可能要嘗試下面的代碼段?

if (file_exists($name)) {
    $reader = new XMLReader();
    if ($reader->open($name)) {
        $reader->setSchema('inc/schema.xsd');
        while($reader->read());
        $reader->close();
    }
} else {
    die("you're having trouble with the files!");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM