Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top