简体   繁体   English

如何在C ++中使用SAX XML解析中使用上下文(使用libxml2)?

[英]how to use contexts in SAX XML parsing in C++(using libxml2)?

I'm writing an xml parser application in C++ using libxml2 library. 我正在使用libxml2库在C ++中编写xml解析器应用程序。 I use startElementNsSAX2Func to parse the elements and charactersSAXFunc to parse element values. 我使用startElementNsSAX2Func解析元素,并使用characterSAXFunc解析元素值。

Signatures: 签名:

void    startElementNsSAX2Func  (void * ctx, const xmlChar * localname, const xmlChar * prefix, const xmlChar * URI, int nb_namespaces, const xmlChar ** namespaces, int nb_attributes, int nb_defaulted, const xmlChar ** attributes)
void    charactersSAXFunc(void * ctx, const xmlChar * ch, int len)

I want to make use of the ctx variable so that I can parse the xml document based on the order of the elements and I'm not sure how to do it. 我想利用ctx变量,以便我可以根据元素的顺序来解析xml文档,但是我不确定该怎么做。 Any insight on this would be really helpful. 关于此的任何见解将非常有帮助。

Also I couldn't find a good article on XML SAX parsing in C/C++. 我也找不到关于C / C ++中XML SAX解析的好文章。 Does anyone know a good tutorial on this? 有人知道这方面的好教程吗?

Thanks for your help! 谢谢你的帮助!

The ctx argument of the SAX callbacks will hold the pointer that is passed as user_data to initialization functions like xmlCreatePushParserCtxt or xmlCreateIOParserCtxt : SAX回调的ctx参数将保存作为user_data传递给xmlCreatePushParserCtxtxmlCreateIOParserCtxt类的初始化函数的指针:

xmlParserCtxtPtr xmlCreatePushParserCtxt (xmlSAXHandlerPtr sax, 
                                          void * user_data, 
                                          const char * chunk, 
                                          int size, 
                                          const char * filename)

It can be used to pass a pointer to an arbitrary user-defined structure. 它可以用于将指针传递给任意用户定义的结构。 This structure will typically contain a state variable that can be used to determine the current position in the document tree. 该结构通常将包含状态变量,该状态变量可用于确定文档树中的当前位置。

A nice tutorial can be found here . 一个很好的教程可以在这里找到 It uses the deprecated SAX1 interface, but the SAX2 interface is similar. 它使用已弃用的SAX1接口,但SAX2接口相似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM