I am using libxml to parse xml response. I found out that libxml detects encoding by reading xml file header. But I could not find how to get that detected encoding.I did the following to get libxml to detect encoding:

xmlCreatePushParserCtxt(&simpleSAXHandlerStruct, (__bridge void *)(self), [data bytes], [data length], NULL);

I came across

xmlDetectCharEncoding(const unsigned char * in, int len)

which returns the detected encoding but that is not from first line of xml file.

Any idea?

有帮助吗?

解决方案

After parsing a document, you can find the encoding in the encoding slot of the xmlDoc struct.

EDIT: If you use a push parser with your own SAX handler, no xmlDoc will be built. In this case, you can get the encoding from an xmlParserCtxtPtr via ctxt->encoding (or maybe ctxt->input->encoding) but only after the XML declaration has been parsed. This means that you have to call xmlParseChunk at least once and make sure that enough data has been fed to the push parser.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top