Creating a string using libxml2 (C ++)

My problem is that I want to create an xml tree and get a simple string object (or even char *). And I can not save xml to file.

So, at the input, I have xmlDocPtr with a full xml tree and you want to get a string containing xml, but without using files.

thanks for attention.

+5
source share
4 answers

Use xmlDocDumpMemory or any of its cousins. Using:

void xml_to_string(xmlDocPtr doc, std::string &out)
{
    xmlChar *s;
    int size;
    xmlDocDumpMemory(doc, &s, &size);
    if (s == NULL)
        throw std::bad_alloc();
    try {
        out = (char *)s;
    } catch (...) {
        xmlFree(s);
        throw;
    }
    xmlFree(s);
}
+10
source

What I wrote earlier. Hope this helps ...

xmlDocPtr pDoc = ... // your xml docuemnt

xmlCharPtr psOutput;
int iSize;
xmlDocDumpFormatMemoryEnc(pDoc, &psOutput, &iSize, "UTF-8", 1);

// psOutput should point to the string.

// Don't forget to free the memory.
xmlFree(psOutput);
+1
source

xmlDocDumpMemory(), . xmlCleanupParser() , xmlDocDumpMemory(). , xmlDocDumpMemory() .

0

I was not able to get exactly what you want, but after changing the value of the node, you can save easily with xmlDocDump.

-1
source

All Articles