LibXML2 Sax Parsing and Ampersand

I encountered (as it seems to me) strange behavior when using the sax parser, and I wanted to know if this was normal.

I am sending this XML through the SAX parser:

<site url="http://example.com/?a=b&amp;b=c"; /> 

"&" is converted to "&" when the startElement is called. Is this supposed to be? If so, I would like to understand why.

I pasted an example demonstrating the problem here:

 #include <stdlib.h> #include <libxml/parser.h> static void start_element(void * ctx, const xmlChar *name, const xmlChar **atts) { int i = 0; while(atts[i] != NULL) { printf("%s\n", atts[i]); i++; } } int main(int argc, char *argv[]) { xmlSAXHandlerPtr handler = calloc(1, sizeof(xmlSAXHandler)); handler->startElement = start_element; char * xml = "<site url=\"http://example.com/?a=b&amp;b=c\" />"; xmlSAXUserParseMemory( handler, NULL, xml, strlen(xml) ); } 

PS: This message is really extracted from the LibXML2 list ... and I am not the original author of this letter, but I noticed a problem with Nokogiri and Aaron (accompanying Nokigiri) actually sent this message myself.

+4
source share
1 answer

This post describes the same problem (which I also have) and the answer says

ask the parser to replace entity values

What does this mean when you customize your context, set this parameter as follows:

 xmlParserCtxtPtr context = xmlCreatePushParserCtxt(&yourSAXHandlerStruct, self, NULL, 0, NULL); xmlCtxtUseOptions(context, XML_PARSE_NOENT); 
+5
source

Source: https://habr.com/ru/post/1286090/


All Articles