7

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

So in the input I have xmlDocPtr with complete xml tree and want to get string containing xml but without using files.

Thx for attention.

4 Answers 4

12

Use xmlDocDumpMemory or any of its cousins. Usage:

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Something I wrote while ago. Hope it 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);

Comments

0

If you use xmlDocDumpMemory() be careful. Call xmlCleanupParser() at the very end, after using xmlDocDumpMemory(). It seems like xmlDocDumpMemory() uses it internaly.

Comments

-1

I couldnt get exactly what you want but after changing the node value you can save it easily with xmlDocDump.

2 Comments

The OP says he does not want to save the XML to a file.
ups.. i think i couldnt get the problem well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.