2

我正在使用 libxml2 针对 xsd 模式验证 xml 文件。使用 xmlSchemaSetParserErrors 函数,错误输出到标准错误。我需要获取这些验证错误,将它们存储在内存中并显示给用户。如何重定向这些错误?你能给我一些例子吗?谢谢,安德里亚

4

3 回答 3

2

或者更简洁:

void err(void *ctx, const char *msg, ...)
{
    va_list args;
    va_start(args, msg);
    char *val = va_arg(args,char*);
    va_end(args);
}

val 现在包含验证错误

于 2012-08-30T08:43:45.483 回答
2

此示例使用解析器模块的验证回调机制。预期的回调签名xmlSchemaSetParserErrors似乎是相同的。

#include <iostream>
#include <cstdarg>
#include <cstdio>
#include <vector>
#include <string>
#include <iterator>
#include <libxml/parser.h>
#include <libxml/tree.h>

struct ParserContext
{
    ParserContext() : context(xmlNewParserCtxt()) {}
    ~ParserContext() { xmlFreeParserCtxt(context); }
    xmlParserCtxtPtr context;
private:
    ParserContext(ParserContext&);
    void operator=(ParserContext&);
};



struct ErrorHandler
{
    std::vector<std::string> errors;

    void RegisterErrorHandling(xmlValidCtxt& validationContext)
    {
        // Change this to register for schema errors...
        validationContext.userData = this;
        validationContext.error = &ErrorHandler::Handle;
    }

private:
    static void Handle(void *handler, const char *format, ...)
    {
        va_list arguments;
        va_start(arguments, format);
        std::string message = MakeMessage(format, arguments);
        va_end(arguments);

        ErrorHandler* errorHandler = static_cast<ErrorHandler*>(handler);
        errorHandler->errors.push_back(message);
    }

    static std::string MakeMessage(const char* format, va_list arguments)
    {
        const size_t bufferSize = 200;
        std::vector<char> buffer(bufferSize, 0);

        size_t charactersWritten = 
            vsnprintf(&buffer.front(), bufferSize, format, arguments);
        if (charactersWritten == -1)
            buffer.back() = 0;  // Message truncated!
        return std::string(&buffer.front());
    }
};


struct XmlDocument
{
    static XmlDocument FromFile(const char* fileName)
    {
        ParserContext parser;
        ErrorHandler errorHandler;
        errorHandler.RegisterErrorHandling(parser.context->vctxt);
        XmlDocument document(xmlCtxtReadFile(
            parser.context, fileName, NULL, XML_PARSE_DTDVALID));
        document.errors = move(errorHandler.errors);
        return document;
    }

    XmlDocument(XmlDocument&& other) : 
        xmlPointer(other.xmlPointer),
        errors(move(other.errors))
    {
        other.xmlPointer = nullptr;
    }

    ~XmlDocument() 
    { 
        xmlFreeDoc(xmlPointer); 
    }

    xmlDocPtr xmlPointer;
    std::vector<std::string> errors;

private:
    XmlDocument(xmlDocPtr pointer) : xmlPointer(pointer) {}
    XmlDocument(XmlDocument&);
    void operator=(XmlDocument&);
};


void DisplayErrorsToUser(
    const XmlDocument& document, 
    std::ostream& displayStream = std::cout)
{
    using namespace std;
    copy(begin(document.errors), end(document.errors),
        ostream_iterator<string>(displayStream, "\n"));
}

int main()
{
    auto xml = XmlDocument::FromFile("test.xml");
    DisplayErrorsToUser(xml);
}
于 2012-08-24T11:41:29.580 回答
1

正如 API 文档中所说,xmlSchemaSetParserErrors() Set the callback functions used to handle errors for a validation context.

您需要针对定义的签名编写两个回调函数:

一个例子可能是:

void err(void *ctx, const char *msg, ...)
{
  char buf[1024];
  va_list args;

  va_start(args, msg);
  int len = vsnprintf_s(buf, sizeof(buf), sizeof(buf)/sizeof(buf[0]), msg, args);
  va_end(args);

  if(len==0) // Can't create schema validity error!
  else       // Do something to store `buf`, 
             // you may need to use void *ctx to achieve this

  return;
}

然后打电话

xmlSchemaSetValidErrors(valid_ctxt_ptr, (xmlSchemaValidityErrorFunc) err, (xmlSchemaValidityWarningFunc) warn, ctx);

打电话之前

xmlSchemaValidateDoc()
于 2012-08-28T13:37:34.740 回答