我有以下 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,我收到以下错误:
Warning: XMLReader::read(): Unimplemented block at ..\xmlschemas.c:28351 in xml2csv.php on line 43
Warning: XMLReader::read(): An Error Occurred while reading in xml2csv.php on line 43
Warning: DOMDocument::load(): Premature end of data in tag Products line 1 in products.xml, line: 532571 in xml2csv.php on line 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 方法返回 bool 值,成功时为 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!");
}
关于php - 在 PHP 中使用 XMLReader 读取 XML 和验证模式时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23501003/