LibXML2 Sax分析和&符号 [英] LibXML2 Sax Parsing and ampersand
问题描述
我遇到(我认为是)使用sax解析器时的一个奇怪的行为,我想知道是否正常。
I've encountered (what I think is) a strange behavior when using the sax parser, and I wanted to know if it's normal.
我正在发送这个XML通过SAX解析器:
I'm sending this XML through the SAX parser:
<site url="http://example.com/?a=b&b=c"; />
&当 startElement
回调
被调用时,转换为&。应该这样做吗?如果是这样,我想要
了解为什么。
The "&" gets converted to " &" when the startElement
callback
is called. Is it supposed to do that? If so, I would like to
understand why.
我已经粘贴了一个示例,说明了这个问题:
I've pasted an example demonstrating the issue 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&b=c\" />";
xmlSAXUserParseMemory( handler,
NULL,
xml,
strlen(xml)
);
}
PS:此消息实际上是从 LibXML2列表 ...我不是这封邮件的初始作者,但我注意到使用 Nokogiri 和 Aaron (Nokogiri的维护者)实际上自己发布了这个消息。
PS: This message is actually extracted from the LibXML2 list... and I am not the initial author of this mail, but I noticed the problem using Nokogiri and Aaron (the maintainer of Nokogiri) actually posted this message himself.
推荐答案
This message describes the same problem (which I had as well) and the response says to
请求解析器替换实体值
ask the parser to replace entities values
意思是当您设置您的上下文时,设置如下选项:
What that means is when you are setting up your context, set the option like this:
xmlParserCtxtPtr context = xmlCreatePushParserCtxt(&yourSAXHandlerStruct, self, NULL, 0, NULL);
xmlCtxtUseOptions(context, XML_PARSE_NOENT);
这篇关于LibXML2 Sax分析和&符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!