如何使用 libxml2 的 SAX 接口来解析流式 xml 文件?

How can I use SAX interface of libxml2 to parse streamed xml file?

我需要从服务器解析大 xml 文件。我们目前使用流来读取这些文件,如下所示:

class stream
{
    size_t getSize();
    size_t read(void* buf, size_t offset, size_t size);
}

但我只能找到两个接口:

XMLPUBFUN int XMLCALL
        xmlSAXUserParseFile (xmlSAXHandlerPtr sax,
                     void *user_data,
                     const char *filename);
XMLPUBFUN int XMLCALL
        xmlSAXUserParseMemory   (xmlSAXHandlerPtr sax,
                     void *user_data,
                     const char *buffer,
                     int size);

我该如何解决这个问题?谢谢

使用 libxml2 的 push parser interface. Initialize the parser by calling xmlCreatePushParserCtxt. Then read your data from the server and process it piece by piece with xmlParseChunk. This will invoke the SAX callbacks. Finally, free the parser context with xmlFreeParserCtxt