【问题标题】:validate XML against XSD with errors针对 XSD 验证 XML 是否有错误
【发布时间】:2012-07-27 08:23:50
【问题描述】:

我想根据 XSD 文件验证 XML,如果有错误,我会得到错误。

当我将它与 DOM 一起使用而不是 XMLReader 时,它可以正常工作。据知道他们都使用 libxml 库,所以我尝试将它用于 XMLReader 但没有运气。

谢谢

DOM(工作正常)

libxml_use_internal_errors(true);

if (! $xml->schemaValidate($xsd_file))
{
    get_errors();
    exit;
}

function get_errors()
{
    $messages   = null;
    $errors     = libxml_get_errors();

    foreach ($errors as $error)
    {
        switch ($error->level)
        {
            case LIBXML_ERR_ERROR:
                $messages .= 'Error ' . $error->code . $error->message;
                break;

            case LIBXML_ERR_WARNING:
                $messages .= 'Warning ' . $error->code . $error->message;
                break;

            case LIBXML_ERR_FATAL:
                $messages .= 'Fatal ' . $error->code . $error->message;
                break;
        }

        echo $messages .= ($error->file) ? $error->file : $error->line;
    }
}

XMLREADER(不适用于上述相同的错误函数)

libxml_use_internal_errors(true);

//This returns true all times whether XML has faults or not
if (! $reader->setSchema($xsd_file))
{
   //This echos nothing whether XML has faults or not
   get_errors();
}

【问题讨论】:

    标签: xml xsd xmlreader libxml2 xml-libxml


    【解决方案1】:

    设置架构后,我看不到任何 read() 或 isValid() 正在进行,那么您将如何得到错误?看看documentation(我假设是PHP),它应该可以帮助您了解一个想法。

    【讨论】:

    • 问题是我在 XML、XSD、解析器等方面非常陌生,所以虽然我阅读了所有这些文档,但我还是有点难以解决。我试图将 isValid() 其他函数放在一起,但没有任何改变。谷歌也是如此。我想我需要更多经验或帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 2011-10-12
    相关资源
    最近更新 更多