Search code examples
c++xmppexpat-parser

Getting "not well-formed (invalid token)" error from expat library


I'm trying to parse some XMPP with expat library. I have input file like:

<iq from='ya.ru' to='foo@ya.ru/Foo1256' id='ping_1' type='get'><ping xmlns='urn:xmpp:ping'/></iq>

But there is error "not well-formed (invalid token)" every time I use XML_Parse(). I've found out that error happens when expat tries to parse "from=...." or other attribute of the "iq"-tag. For example, this string is parsed without error:

<iq><ping xmlns='urn:xmpp:ping'/></iq>

Why?

Parser is created with XML_ParserCreateNS(NULL, '|');.

This is parsing function:

bool CSessionTreeItem::setXMLForParser(char *buf, int Len, AnsiString &Error)
{
    bool error = false;
    for(int i = 0; i < Len; i ++)
        {
            if(i + 5 < Len)
                {
                    if(memcmp(buf + i, "<?xml", 5) == 0)
                    {
                            XML_ParserReset(parser, NULL);
                            InitHandlers();
                    }
                }
                if (!XML_Parse (parser, buf + i, 1, 0))
                {
                        Error = AnsiString(XML_ErrorString (XML_GetErrorCode (parser)));
                        error = true;
                        break;
                }
                if(endCompressTag)
                {
                    endCompressTag = false;
                    if(i + 1 < Len)
                    {
                        if(write(buf + i + 1, Len - (i + 2), Error) != 0)
                        {
                            error = true;
                            break;
                        }
                    }
                    break;
                }
        }
        return (!error);
}
  • C++ Builder 6
  • Expat 2.0.1

Solution

  • In my case using Expat 2.1.0 solved the problem.