Unable to call startDocument and StartElement

Hi team I am using SAX parser in my code . but after adding logs I found out its not calling startDocument and startElement

memset(&saxHandler, 0, sizeof(xmlSAXHandler));
// Create an instance of XmlParser
parser = new XmlParser();
// Create the parser context
parserCtx = xmlCreatePushParserCtxt(&saxHandler, parser, NULL, 0, NULL);
saxHandler.startDocument = &(XmlParser::staticStartDocument);
saxHandler.endDocument = &(XmlParser::staticEndDocument);
saxHandler.startElement = &(XmlParser::staticStartElement);
saxHandler.endElement = &(XmlParser::staticEndElement);
saxHandler.characters = &(XmlParser::staticCharacters);

 saxHandler.warning = &(XmlParser::staticWarning);
 saxHandler.error = &(XmlParser::staticError);

please let me know am I doing it write ? or there is some other approch

earlier the entire code use xerces-c to parser but now I replaced it with libxml2

Please refer to libxml2 tutorial, more specifically the SAX Interface debugging section.

I am making the iso and installing it on a different machine, how can I use the debugger there any idea?

I guess you should debug & fix the issue on a development machine ( before building the iso ).

Okay one more thing
In my code we are doing like this

So, a static wrapper function (For eg: static void staticStartElement() was created which internally calls startElement)

Will it work ? as I have read
xmlSAXHandler requires all functions like startElement, startDocument etc. to be static.

But we cannot declare them to be static as we are accessing non-static variables inside them.

and I am getting → after running
xmllint --sax XmlParser.C
SAX.setDocumentLocator()
SAX.startDocument()
SAX.error: Start tag expected, ‘<’ not found
SAX.endDocument()

xmllint --sax XmlParser.C

The input should be an XML file.

The --sax option is used to debug / observe how the sax interface parses the XML nodes.

All SAX handler methods accept an additional “user data” pointer which can be used for additional state (variables). In general, most applications will use a struct to carry around local state. This blog post explains a bit more: Using the SAX Interface of LibXML | James Henstridge

Noted @Sid , in my case xml format is not the basic one its converting into tags and all …

@flavorjones I have also declared staticStartElement / all static functions in different file, could you please explain a bit more what can be the potential issue? I have gone through the link you provided

  1. You should get the minimal case working first (before splitting code across multiple files).

  2. You should post the minimal example code here which doesn’t work.

Thanks ,I debugged it Now it started calling static function and startDocument,startElement too . But there is one more issue could please suggest on this
After creating the parserCtx using xmlCreatePushParserCtxt Like :-

parserCtx = xmlCreatePushParserCtxt(&saxHandler, parser, NULL, 0, NULL);
int parseStatus = xmlParseChunk(parserCtx, xml, strlen(xml),0);

My code crashes at XmlParserChunk

please suggest where I am doing it wrong

In order to help you, we need more information. Please share your complete working code and the input; or else share the stack walkback or debugger frames from the crash.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.