Search code examples
c++libxml2

libxml2 in C++, how to set encoding


I am creating a xml document using libxml2 in C++. After xml file is created, i am seeing the following line in the top of the xml buffer.

<?xml version="1.0" encoding="ISO-8859-1"?>

I have not specified any encoding while creating the doc. Some of the xml content ascii value is greater than 127 which is not handled by ISO-8859-1 encoding. For e.g: I have pound character (£) in my xml content. So whenever it encounters ascii value greater than 127 it breaks and my xml file is incomplete. I read from some forums saying that changing the encoding to UTF-8 will handle this kind of scenarios. But I don't know how to specify encoding for the xml doc creation. Following is the sample code I have used.

xmlDoc *doc = NULL;
xmlChar *xmlbuff = NULL;
int buffersize;
xmlNodePtr pNode;
doc = xmlNewDoc(NULL);
pNode = xmlNewNode(0, (const xmlChar*)"Temp");
xmlDocSetRootElement(doc, pNode);
xmlSetProp(pNode, (const xmlChar*)"Item", (const xmlChar*)"Office£");
xmlDocDumpMemory(doc, &xmlbuff, &buffersize);
xmlFree(xmlbuff);
xmlFreeDoc(doc);
doc = NULL;

output:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Temp Item="Office

Solution

  • I suppose that you should use xmlDocDumpMemoryEnc() (or xmlDocDumpFormatMemoryEnc(), if you want control the indentation too) instead xmlDocDumpMemory().

    Something like

    xmlDocDumpMemoryEnc(doc, &xmlbuff, &buffersize, "UTF-8");