DOMDocument for string xerces-c

I have parsed an XML document using xerces-c and it can be written to a file as an example of DOMPrint, but I cannot save it in an array. I see online that I still have to use a serializer, but I'm not sure what to change. Obviously, instead of using LocalFileFormatTarget, I should use something else, but searching the Internet for a reference to MemBufFormatTarget gives no idea how to use it. How can I get an xml document in a string using xerces-c?

+3
source share
1 answer

Use an XMLFormatTarget class like this to get output to the character buffer:

class LStringXMLFormatTarget : public XMLFormatTarget
{
public:
LStringXMLFormatTarget()
{
    m_pBuffer = NULL;
    m_nTotal = 0;
}

char*       GetBuffer()
{
    return m_pBuffer;
}

ULONG       GetLength()
{
    return m_nTotal;
}

virtual void writeChars(const XMLByte* const toWrite, const XMLSize_t count, XMLFormatter* const formatter)
{
    if(toWrite)
    {
        char*   pTmp = new char[m_nTotal + count + 1];

        if(m_pBuffer)
        {
            memcpy(pTmp, m_pBuffer, m_nTotal);
            delete m_pBuffer;
        }

        memcpy(&pTmp[m_nTotal], toWrite, count);

        m_nTotal += count;
        m_pBuffer = pTmp;

        if(m_pBuffer)
            m_pBuffer[m_nTotal] = 0;
    }
}

protected:
    char*       m_pBuffer;
    ULONG       m_nTotal;
};

, , .

DOMLSOutput DOMLSSerializer:

DOMLSOutput*  pLSOutput = impl->createLSOutput();

if(pLSOutput)
{
    pLSOutput->setByteStream(&stringTarget);
    pSerializer->write(doc, pLSOutput);
}

p.s. , writeChars() , ... - XMLFormatTarget.

+3

Source: https://habr.com/ru/post/1779236/


All Articles