1

I have a TCP connection that sends me XML messages over a stream.

The first message I receive in the <?xml version="1.0" encoding="utf-8"?> message.

The second is a authentication request message, which provides a seed to use when hashing my credentials to send back to the server - <session seed="VJAWKBJXJO">.

At this point I should send a <session user="admin" password_hash="123456789"> message back to authenticate myself.

Once authenticated I will receive the desired data in the form of <Msg>data</Msg>.

If I do not authenticate in time with the server, I receive a </session> message, to indicate the session has been closed.

The problem is that I can't use a DOM parser because attempting to parse the <session> tag with no end tag always throws an error, so I'm attempting to use the Xerces-c SAX parser, to perform progressive parsing of the XML.

When I receive each message I want to ideally append it to a MemBufInputSource which contains all XML which has currently been received, then perform a parseNext on the buffer to parse the new XML that has been received, but I can't figure out how to get it working correctly.

Is there a better way around this problem? Perhaps just using a special case for the <session></session> messages?

Thanks

Sean Chapman
  • 322
  • 1
  • 10
  • My current implementation keeps falling over whenever it tries to parse the `` message when it can't find the `` end tag if that helps anyone – Sean Chapman Mar 20 '12 at 14:00

1 Answers1

0

Have you tried using a different parser? If not, I'm using libxml2 (http://xmlsoft.org/), it's incredibly simple and it allows you to handle errors at your leisure.

You can create an xmlTextReaderPtr from a stream (your connection):

xmlTextReaderPtr reader = xmlReaderForMemory(...)

Then iterate through the nodes until you find your data:

while ( (result=xmlTextReaderRead(reader))== 1 )
{
    int nodetype = xmlTextReaderNodeType(reader);

    if ( nodetype == XML_READER_TYPE_ELEMENT )
    {
        const xmlChar* name = xmlTextReaderConstName(reader);
        /* now name is the name of the element, like "session" */
        if ( strcmp(name,"session")==0 )
        {
            /* now look for the XML_READER_TYPE_ATTRIBUTE named "seed" and read the
             * value with xmlTextReaderConstValue to get the seed value */
        }
    }
}

They have a simple example, as well, for parsing out values:

http://xmlsoft.org/examples/reader1.c

It does have a bunch of features in there, though I can only speak for the basic reading, writing, and xinclude features.

Hope that helps!

lee-m
  • 2,269
  • 17
  • 29
rutgersmike
  • 1,183
  • 9
  • 18