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

标签 php c xml xmlreader

我有以下 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/

相关文章:

xml - xslt为每个嵌套嵌套,用逗号分隔列表

java - setContentView 上的另一个 Resources$NotFoundException

php - HTML 字符串中所有字符的字符数,但仅测量 20 个可见单词

php - 如何从 Smarty 中访问 Mysql 结果集?

ios - 如何以编程方式终止 iOS 中的进程

xml - Perl LibXML 打印额外的 "text"标签

php - 如何用 aes-256-ecb 加密的 as3crypto 字符串解密?

php - 在 Codeception 套件完成后运行自定义代码

c - Lua C API - 将属性映射到函数

c - 循环中的空间局部性