c -在PHP中读取XML并使用XMLReader验证模式时出错


c - 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!");
}

对于某些url我得到以下错误:

警告:XMLReader::read():未实现块at.'xmlschema .c:28351在xml2csv.php联机43

警告:XMLReader::read():读取时发生错误Xml2csv.php第43行

警告:DOMDocument::load():标签Products中的数据过早结束在products.xml中的第1行,行:xml2csv.php第55行

大多数情况下,给我这个问题的url是本地url(当$name = "file.xml")或远程url(当$name = "http://www.domain.com/products.xml")被破坏(返回404,500等)。

一个简单的谷歌搜索错误带给我这个:https://github.com/Chronic-Dev/libxml2/blob/master/xmlschemas.c

在上面提到的28351行,似乎有一个文本说TODO。因为我的C语言技能非常有限(几乎没有),我想更好地理解这个错误的原因,以及我如何避免碰到它。

如XMLReader所述,open方法返回布尔值,成功时为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!");
}