W3C home > Mailing lists > Public > www-lib@w3.org > July to September 2001

get it while it's hot - raw XML

From: Josh Watts <jwatts@pretorynet.com>
Date: Tue, 17 Jul 2001 16:12:12 -0400
Message-ID: <1EB9FCBE69B6154099DF0C4121D1503618A42E@pnisvrms001.pretorynet.com>
To: <www-lib@w3.org>
I think I've found a good clean way to get raw XML from libwww. It's
really easy too.

First, there is another way of getting raw XML that's much much simpler.
You just have to set the request's output format to WWW_RAW:

HTRequest_setOutputFormat(request,WWW_RAW);

You'll also get the headers and byte ranges. However, since I want
libwww to handle authentication for me, WWW_RAW isn't an option. I use
WWW_SOURCE because it'll invoke the MIME handlers in libwww that will
handle adding authentication to requests. Unfortunately for me, it also
wants to process an XML document (Content-Type: text/xml). 

First, you've got to set up the callback function that's called when the
expat XML parser (hereafter referred to as "XML parser") is created.

HTXMLCallback_registerNew (HTXML_newInstance, NULL);

When the XML parser is created, it calls the callback function to
register even more callback functions for the XML parser. My
HTXML_newInstance function looks like this:

PRIVATE void HTXML_newInstance (HTStream *		me,
				HTRequest *		request,
				HTFormat 		target_format,
				HTStream *		target_stream,
				XML_Parser              xmlparser,
				void * 			context)
{
    if (me != NULL && 
        xmlparser != NULL)
    {
        XML_SetDefaultHandler(xmlparser, XML_default);
    }
}

I found a good article on the expat parser
(http://www.xml.com/pub/a/1999/09/expat/index.html) but all we need to
know is that whatever callback we setup with XML_SetDefaultHandler will
be called first before any parsing is actually done. I don't have any
definitive proof on this though - it's simply what I've noticed from
running it in the debugger.

My XML_default function looks like this:

PRIVATE void XML_default (void * userData,
			  const XML_Char * s, int len)
{
    if (xmlData == "")
    {
        xmlData = s;
    }
    return;
}

where xmlData is a string object from the STL; xmlData is also a global
variable, which I hate, but it'll have to do until I refine the code.
That's all there is to it although I should destroy the XML parser at
this point since I don't want it to contiue processing.


=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
Josh Watts  /  PretoryNet, Inc.
email: jwatts@pretorynet.com
Received on Tuesday, 17 July 2001 16:12:43 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 23 April 2007 18:18:39 GMT