6

saxパーサーを使用しているときに(私が思うに)奇妙な動作に遭遇しました。それが正常かどうかを知りたいと思いました。

このXMLをSAXパーサーを介して送信しています。

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

startElementコールバックが呼び出されると、「&」は「&」に変換されます。それをすることになっていますか?もしそうなら、私はその理由を理解したいと思います。

ここに問題を示す例を貼り付けました。

#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:このメッセージは実際にはLibXML2リストから抽出されています...そして私はこのメールの最初の作成者ではありませんが、Nokogiriの使用上の問題に気づき、AaronNokogiriのメンテナ)が実際にこのメッセージを自分で投稿しました。

4

1 に答える 1

5

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);
于 2009-09-02T16:17:23.820 に答える